From 6bc7acd8e2292ce428174a29bf32e409c3f5eca2 Mon Sep 17 00:00:00 2001 From: vng Date: Tue, 12 Apr 2011 07:10:15 +0300 Subject: [PATCH] First version of guide page. Need to check for sl::Str function policies. --- qt/guide_page.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++ qt/guide_page.hpp | 40 +++++++++++++++++++ qt/mainwindow.cpp | 41 ++++++++++++++++---- qt/mainwindow.hpp | 8 +++- qt/qt.pro | 10 +++-- 5 files changed, 185 insertions(+), 13 deletions(-) create mode 100644 qt/guide_page.cpp create mode 100644 qt/guide_page.hpp diff --git a/qt/guide_page.cpp b/qt/guide_page.cpp new file mode 100644 index 0000000000..310f6c47ae --- /dev/null +++ b/qt/guide_page.cpp @@ -0,0 +1,99 @@ +#include "guide_page.hpp" + +#include "../platform/platform.hpp" + +#include "../coding/strutil.hpp" + +#include + +#include +#include + + +namespace qt { + +GuidePageHolder::GuidePageHolder(QWidget * pParent) + : base_type(pParent) +{ + QVBoxLayout * pLayout = new QVBoxLayout(this); + pLayout->setContentsMargins(0, 0, 0, 0); + + m_pEditor = new QLineEdit(this); + + m_pView = new QWebView(this); + + connect(m_pEditor, SIGNAL(returnPressed()), this, SLOT(OnShowPage())); + + pLayout->addWidget(m_pEditor); + pLayout->addWidget(m_pView); + setLayout(pLayout); + + CreateEngine(); +} + +void GuidePageHolder::showEvent(QShowEvent * e) +{ + base_type::showEvent(e); + + m_pEditor->setFocus(); +} + +namespace +{ + sl::StrFn::Str const * StrCreate(char const * pUtf8Data, uint32_t sz) + { + wstring * s = new wstring(); + *s = FromUtf8(string(pUtf8Data)); + return reinterpret_cast(s); + } + + void StrDestroy(sl::StrFn::Str const * p) + { + delete reinterpret_cast(p); + } + + int StrPrimaryCompare(void *, sl::StrFn::Str const * a, sl::StrFn::Str const * b) + { + wstring const * pA = reinterpret_cast(a); + wstring const * pB = reinterpret_cast(b); + return *pA == *pB; + } + + int StrSecondaryCompare(void *, sl::StrFn::Str const * a, sl::StrFn::Str const * b) + { + wstring const * pA = reinterpret_cast(a); + wstring const * pB = reinterpret_cast(b); + return *pA == *pB; + } +} + +void GuidePageHolder::CreateEngine() +{ + string const dicPath = GetPlatform().ReadPathForFile("dictionary.slf"); + string const indPath = GetPlatform().WritableDir() + "index"; + + sl::StrFn fn; + fn.Create = &StrCreate; + fn.Destroy = &StrDestroy; + fn.PrimaryCompare = &StrPrimaryCompare; + fn.SecondaryCompare = &StrSecondaryCompare; + + fn.m_pData = 0; + fn.m_PrimaryCompareId = 1; + fn.m_SecondaryCompareId = 2; + + m_pEngine.reset(new sl::SloynikEngine(dicPath, indPath, fn)); +} + +void GuidePageHolder::OnShowPage() +{ + sl::SloynikEngine::SearchResult res; + m_pEngine->Search(m_pEditor->text().toStdString(), res); + + sl::SloynikEngine::ArticleData data; + m_pEngine->GetArticleData(res.m_FirstMatched, data); + + m_pView->setHtml(QString::fromUtf8(data.m_HTML.c_str())); +} + +} diff --git a/qt/guide_page.hpp b/qt/guide_page.hpp new file mode 100644 index 0000000000..bbef7b321b --- /dev/null +++ b/qt/guide_page.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include "../words/sloynik_engine.hpp" + +#include "../std/scoped_ptr.hpp" + +#include + + +class QLineEdit; +class QWebView; + +namespace sl { class SloynikEngine; } + +namespace qt +{ + class GuidePageHolder : public QWidget + { + typedef QWidget base_type; + + Q_OBJECT; + + public: + GuidePageHolder(QWidget * pParent); + + protected: + void CreateEngine(); + + virtual void showEvent(QShowEvent * e); + + protected Q_SLOTS: + void OnShowPage(); + + private: + QLineEdit * m_pEditor; + QWebView * m_pView; + + scoped_ptr m_pEngine; + }; +} diff --git a/qt/mainwindow.cpp b/qt/mainwindow.cpp index 0aa8c8c79b..c3c11f6ca7 100644 --- a/qt/mainwindow.cpp +++ b/qt/mainwindow.cpp @@ -9,6 +9,7 @@ #include "about.hpp" #include "preferences_dialog.hpp" #include "info_dialog.hpp" +#include "guide_page.hpp" #include "../defines.hpp" @@ -21,6 +22,7 @@ #include #include #include + #include #include "../base/start_mem_debug.hpp" @@ -38,6 +40,7 @@ MainWindow::MainWindow() : m_updateDialog(0) CreateNavigationBar(); CreateClassifPanel(); + CreateGuidePanel(); setCentralWidget(m_pDrawWidget); @@ -160,22 +163,39 @@ void MainWindow::LoadState() void MainWindow::CreateClassifPanel() { - m_pClassifDock = new QDockWidget(tr("Classificator Bar"), this); + CreatePanelImpl(0, tr("Classificator Bar"), + QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C), SLOT(ShowClassifPanel())); - ClassifTreeHolder * pCTree = new ClassifTreeHolder(m_pClassifDock, m_pDrawWidget, SLOT(Repaint())); + ClassifTreeHolder * pCTree = new ClassifTreeHolder(m_Docks[0], m_pDrawWidget, SLOT(Repaint())); pCTree->SetRoot(classif().GetMutableRoot()); - m_pClassifDock->setWidget(pCTree); + m_Docks[0]->setWidget(pCTree); +} - addDockWidget(Qt::LeftDockWidgetArea, m_pClassifDock); +void MainWindow::CreateGuidePanel() +{ + CreatePanelImpl(1, tr("Guide Bar"), + QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_G), SLOT(ShowGuidePanel())); + + qt::GuidePageHolder * pGPage = new qt::GuidePageHolder(m_Docks[1]); + + m_Docks[1]->setWidget(pGPage); +} + +void MainWindow::CreatePanelImpl(size_t i, QString const & name, + QKeySequence const & hotkey, char const * slot) +{ + m_Docks[i] = new QDockWidget(name, this); + + addDockWidget(Qt::LeftDockWidgetArea, m_Docks[i]); // hide by default - m_pClassifDock->hide(); + m_Docks[i]->hide(); // register a hotkey to show classificator panel QAction * pAct = new QAction(this); - pAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C)); - connect(pAct, SIGNAL(triggered()), this, SLOT(ShowClassifPanel())); + pAct->setShortcut(hotkey); + connect(pAct, SIGNAL(triggered()), this, slot); addAction(pAct); } @@ -283,7 +303,12 @@ void MainWindow::ShowUpdateDialog() void MainWindow::ShowClassifPanel() { - m_pClassifDock->show(); + m_Docks[0]->show(); +} + +void MainWindow::ShowGuidePanel() +{ + m_Docks[1]->show(); } void MainWindow::OnAbout() diff --git a/qt/mainwindow.hpp b/qt/mainwindow.hpp index 3ba37d0ed4..1d20e99c0b 100644 --- a/qt/mainwindow.hpp +++ b/qt/mainwindow.hpp @@ -17,7 +17,7 @@ namespace qt class MainWindow : public QMainWindow { DrawWidget * m_pDrawWidget; - QDockWidget * m_pClassifDock; + QDockWidget * m_Docks[2]; //FindTableWnd * m_pFindTable; UpdateDialog * m_updateDialog; @@ -35,7 +35,12 @@ namespace qt void LoadState(); protected: + void CreatePanelImpl(size_t i, QString const & name, QKeySequence const & hotkey, + char const * slot); + void CreateClassifPanel(); + void CreateGuidePanel(); + void CreateNavigationBar(); //void CreateFindTable(QLayout * pLayout); #if defined(Q_WS_WIN) @@ -48,6 +53,7 @@ namespace qt //void OnFeatureClicked(int row, int col); void ShowUpdateDialog(); void ShowClassifPanel(); + void ShowGuidePanel(); void OnAbout(); void OnPreferences(); }; diff --git a/qt/qt.pro b/qt/qt.pro index 26bbdecd7e..5d585090eb 100644 --- a/qt/qt.pro +++ b/qt/qt.pro @@ -1,13 +1,13 @@ # Main application in qt. ROOT_DIR = .. -DEPENDENCIES = map storage indexer yg platform geometry coding base freetype expat fribidi tomcrypt version +DEPENDENCIES = words map storage indexer yg platform geometry coding base bzip2 freetype expat fribidi tomcrypt version include($$ROOT_DIR/common.pri) TARGET = MapsWithMe TEMPLATE = app -QT *= core gui opengl network +QT *= core gui opengl network webkit win32 { LIBS += -lopengl32 -lws2_32 -lshell32 @@ -82,7 +82,8 @@ SOURCES += \ slider_ctrl.cpp \ about.cpp \ preferences_dialog.cpp \ - info_dialog.cpp + info_dialog.cpp \ + guide_page.cpp HEADERS += \ mainwindow.hpp \ @@ -96,6 +97,7 @@ HEADERS += \ slider_ctrl.hpp \ about.hpp \ preferences_dialog.hpp \ - info_dialog.hpp + info_dialog.hpp \ + guide_page.hpp RESOURCES += res/resources.qrc \