[qt] Rewrote search panel

This commit is contained in:
Alex Zolotarev 2011-06-02 18:10:52 +02:00 committed by Alex Zolotarev
parent d96470fdb6
commit 9fc7593b86
5 changed files with 126 additions and 88 deletions

View file

@ -3,11 +3,10 @@
#include "slider_ctrl.hpp"
#include "about.hpp"
#include "preferences_dialog.hpp"
#include "search_panel.hpp"
#include "../defines.hpp"
#include "../search/result.hpp"
#include "../map/settings.hpp"
#include "../std/bind.hpp"
@ -17,9 +16,6 @@
#include <QtGui/QAction>
#include <QtGui/QMenuBar>
#include <QtGui/QMenu>
#include <QtGui/QLineEdit>
#include <QtGui/QHeaderView>
#include <QtGui/QTableWidget>
#define IDM_ABOUT_DIALOG 1001
#define IDM_PREFERENCES_DIALOG 1002
@ -341,62 +337,9 @@ void MainWindow::OnSearchShortcutPressed()
{
m_pSearchAction->setChecked(true);
m_Docks[2]->show();
m_Docks[2]->widget()->setFocus();
}
}
void MainWindow::OnSearchTextChanged(QString const & str)
{
// clear old results
QTableWidget * table = static_cast<QTableWidget *>(m_Docks[3]->widget());
table->clear();
table->setRowCount(0);
QString const normalized = str.normalized(QString::NormalizationForm_KC);
if (!normalized.isEmpty())
m_pDrawWidget->Search(normalized.toUtf8().constData(),
bind(&MainWindow::OnSearchResult, this, _1));
}
void MainWindow::OnSearchResult(search::Result const & result)
{
if (result.GetString().empty()) // last element
{
if (!m_Docks[3]->isVisible())
m_Docks[3]->show();
}
else
{
QTableWidget * table = static_cast<QTableWidget *>(m_Docks[3]->widget());
int const rowCount = table->rowCount();
table->setRowCount(rowCount + 1);
QTableWidgetItem * item = new QTableWidgetItem(QString::fromUtf8(result.GetString().c_str()));
item->setData(Qt::UserRole, QRectF(QPointF(result.GetRect().minX(), result.GetRect().maxY()),
QPointF(result.GetRect().maxX(), result.GetRect().minY())));
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
table->setItem(rowCount, 0, item);
}
}
void MainWindow::OnSearchPanelShortcutPressed()
{
if (m_Docks[3]->isVisible())
m_Docks[3]->hide();
else
m_Docks[3]->show();
}
void MainWindow::OnSearchPanelItemClicked(int row, int)
{
// center viewport on clicked item
QTableWidget * table = static_cast<QTableWidget *>(m_Docks[3]->widget());
QRectF rect = table->item(row, 0)->data(Qt::UserRole).toRectF();
m2::RectD r2(rect.left(), rect.bottom(), rect.right(), rect.top());
// r2.Inflate(0.0001, 0.0001);
m_pDrawWidget->ShowFeature(r2);
}
void MainWindow::OnPreferences()
{
bool autoUpdatesEnabled = DEFAULT_AUTO_UPDATES_ENABLED;
@ -450,31 +393,10 @@ void MainWindow::CreateGuidePanel()
void MainWindow::CreateSearchBarAndPanel()
{
CreatePanelImpl(2, Qt::TopDockWidgetArea, tr("Search Bar"),
QKeySequence(Qt::CTRL + Qt::Key_F), SLOT(OnSearchShortcutPressed()));
QLineEdit * editor = new QLineEdit(m_Docks[2]);
connect(editor, SIGNAL(textChanged(QString const &)), this, SLOT(OnSearchTextChanged(QString const &)));
m_Docks[2]->setFeatures(QDockWidget::NoDockWidgetFeatures);
// @TODO remove search bar title
m_Docks[2]->setWidget(editor);
// also create search results panel
CreatePanelImpl(3, Qt::LeftDockWidgetArea, tr("Search Results"),
QKeySequence(Qt::CTRL + Qt::ShiftModifier + Qt::Key_F),
SLOT(OnSearchPanelShortcutPressed()));
QTableWidget * panel = new QTableWidget(0, 2, m_Docks[3]);
panel->setAlternatingRowColors(true);
panel->setShowGrid(false);
panel->setSelectionBehavior(QAbstractItemView::SelectRows);
panel->verticalHeader()->setVisible(false);
panel->horizontalHeader()->setVisible(false);
panel->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
connect(panel, SIGNAL(cellClicked(int,int)), this, SLOT(OnSearchPanelItemClicked(int,int)));
m_Docks[3]->setWidget(panel);
CreatePanelImpl(2, Qt::RightDockWidgetArea, tr("Search"),
QKeySequence(QKeySequence::Find), SLOT(OnSearchShortcutPressed()));
SearchPanel * panel = new SearchPanel(m_pDrawWidget, m_Docks[2]);
m_Docks[2]->setWidget(panel);
}
void MainWindow::CreatePanelImpl(size_t i, Qt::DockWidgetArea area, QString const & name,

View file

@ -19,7 +19,7 @@ namespace qt
QAction * m_pSearchAction;
DrawWidget * m_pDrawWidget;
QDockWidget * m_Docks[4];
QDockWidget * m_Docks[3];
#ifndef NO_DOWNLOADER
UpdateDialog * m_updateDialog;
@ -40,7 +40,6 @@ namespace qt
private:
void OnLocationFound();
void OnSearchResult(search::Result const & result);
protected:
#ifndef NO_DOWNLOADER
@ -58,7 +57,6 @@ namespace qt
#endif
protected Q_SLOTS:
void OnSearchTextChanged(QString const &);
#ifndef NO_DOWNLOADER
void ShowUpdateDialog();
void ShowClassifPanel();
@ -69,7 +67,5 @@ namespace qt
void OnMyPosition();
void OnSearchButtonClicked();
void OnSearchShortcutPressed();
void OnSearchPanelShortcutPressed();
void OnSearchPanelItemClicked(int row, int column);
};
}

View file

@ -69,6 +69,7 @@ SOURCES += \
about.cpp \
info_dialog.cpp \
preferences_dialog.cpp \
search_panel.cpp \
HEADERS += \
mainwindow.hpp \
@ -80,6 +81,7 @@ HEADERS += \
about.hpp \
info_dialog.hpp \
preferences_dialog.hpp \
search_panel.hpp \
RESOURCES += res/resources.qrc

82
qt/search_panel.cpp Normal file
View file

@ -0,0 +1,82 @@
#include "search_panel.hpp"
#include "draw_widget.hpp"
#include "../search/result.hpp"
#include "../std/bind.hpp"
#include <QtGui/QHeaderView>
#include <QtGui/QTableWidget>
#include <QtGui/QLineEdit>
#include <QtGui/QVBoxLayout>
namespace qt
{
SearchPanel::SearchPanel(DrawWidget * drawWidget, QWidget * parent)
: QWidget(parent), m_pDrawWidget(drawWidget)
{
m_pEditor = new QLineEdit(this);
connect(m_pEditor, SIGNAL(textChanged(QString const &)), this, SLOT(OnSearchTextChanged(QString const &)));
m_pTable = new QTableWidget(0, 2, this);
m_pTable->setAlternatingRowColors(true);
m_pTable->setShowGrid(false);
m_pTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pTable->verticalHeader()->setVisible(false);
m_pTable->horizontalHeader()->setVisible(false);
m_pTable->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
connect(m_pTable, SIGNAL(cellClicked(int,int)), this, SLOT(OnSearchPanelItemClicked(int,int)));
QVBoxLayout * verticalLayout = new QVBoxLayout();
verticalLayout->addWidget(m_pEditor);
verticalLayout->addWidget(m_pTable);
setLayout(verticalLayout);
}
void SearchPanel::OnSearchResult(search::Result const & result)
{
if (!result.GetString().empty()) // last element
{
int const rowCount = m_pTable->rowCount();
m_pTable->setRowCount(rowCount + 1);
QTableWidgetItem * item = new QTableWidgetItem(QString::fromUtf8(result.GetString().c_str()));
item->setData(Qt::UserRole, QRectF(QPointF(result.GetRect().minX(), result.GetRect().maxY()),
QPointF(result.GetRect().maxX(), result.GetRect().minY())));
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
m_pTable->setItem(rowCount, 0, item);
}
}
void SearchPanel::OnSearchTextChanged(QString const & str)
{
// clear old results
m_pTable->clear();
m_pTable->setRowCount(0);
QString const normalized = str.normalized(QString::NormalizationForm_KC);
if (!normalized.isEmpty())
m_pDrawWidget->Search(normalized.toUtf8().constData(),
bind(&SearchPanel::OnSearchResult, this, _1));
}
void SearchPanel::OnSearchPanelItemClicked(int row, int)
{
// center viewport on clicked item
QRectF const rect = m_pTable->item(row, 0)->data(Qt::UserRole).toRectF();
m2::RectD const r2(rect.left(), rect.bottom(), rect.right(), rect.top());
m_pDrawWidget->ShowFeature(r2);
}
void SearchPanel::showEvent(QShowEvent *)
{
m_pEditor->setFocus();
}
void SearchPanel::hideEvent(QHideEvent *)
{
m_pEditor->clearFocus();
}
}

36
qt/search_panel.hpp Normal file
View file

@ -0,0 +1,36 @@
#pragma once
#include <QWidget>
namespace search { class Result; }
class QTableWidget;
class QLineEdit;
namespace qt
{
class DrawWidget;
class SearchPanel : public QWidget
{
DrawWidget * m_pDrawWidget;
QTableWidget * m_pTable;
QLineEdit * m_pEditor;
Q_OBJECT
private:
void OnSearchResult(search::Result const & result);
protected:
virtual void showEvent(QShowEvent *);
virtual void hideEvent(QHideEvent *);
public:
explicit SearchPanel(DrawWidget * drawWidget, QWidget * parent);
protected slots:
void OnSearchPanelItemClicked(int row, int column);
void OnSearchTextChanged(QString const &);
};
}