From 7d007bcb8ff6942db26f7207f8706fdafba06f9d Mon Sep 17 00:00:00 2001 From: dockes Date: Fri, 20 Jul 2007 10:55:05 +0000 Subject: [PATCH] cleaned up preview window interface --- src/qtgui/preview_w.cpp | 80 +++++++++++++++++++++++------------------ src/qtgui/preview_w.h | 69 ++++++++++++++++++----------------- src/qtgui/rclmain_w.cpp | 48 +++++++++++++------------ 3 files changed, 106 insertions(+), 91 deletions(-) diff --git a/src/qtgui/preview_w.cpp b/src/qtgui/preview_w.cpp index 42839b3d..b3446586 100644 --- a/src/qtgui/preview_w.cpp +++ b/src/qtgui/preview_w.cpp @@ -1,5 +1,5 @@ #ifndef lint -static char rcsid[] = "@(#$Id: preview_w.cpp,v 1.23 2007-07-13 06:31:30 dockes Exp $ (C) 2005 J.F.Dockes"; +static char rcsid[] = "@(#$Id: preview_w.cpp,v 1.24 2007-07-20 10:55:04 dockes Exp $ (C) 2005 J.F.Dockes"; #endif /* * This program is free software; you can redistribute it and/or modify @@ -89,15 +89,16 @@ void Preview::init() this, SLOT(currentChanged(QWidget *))); connect(bt, SIGNAL(clicked()), this, SLOT(closeCurrentTab())); - dynSearchActive = false; - canBeep = true; - tabData.push_back(TabData(pvTab->currentPage())); - currentW = 0; + m_dynSearchActive = false; + m_canBeep = true; + m_tabData.push_back(TabData(pvTab->currentPage())); + m_currentW = 0; if (prefs.pvwidth > 100) { resize(prefs.pvwidth, prefs.pvheight); } m_loading = false; currentChanged(pvTab->currentPage()); + m_justCreated = true; } void Preview::closeEvent(QCloseEvent *e) @@ -148,7 +149,7 @@ bool Preview::eventFilter(QObject *target, QEvent *event) // LOGDEB(("Preview::eventFilter: got ^W\n")); closeCurrentTab(); return true; - } else if (dynSearchActive) { + } else if (m_dynSearchActive) { if (keyEvent->key() == Qt::Key_F3) { doSearch(searchTextLine->text(), true, false); return true; @@ -164,7 +165,7 @@ bool Preview::eventFilter(QObject *target, QEvent *event) if (e && target == e) { if (keyEvent->key() == Qt::Key_Slash) { searchTextLine->setFocus(); - dynSearchActive = true; + m_dynSearchActive = true; return true; } else if (keyEvent->key() == Qt::Key_Space) { e->scrollBy(0, e->visibleHeight()); @@ -183,12 +184,12 @@ void Preview::searchTextLine_textChanged(const QString & text) { LOGDEB1(("search line text changed. text: '%s'\n", text.ascii())); if (text.isEmpty()) { - dynSearchActive = false; + m_dynSearchActive = false; // nextButton->setEnabled(false); // prevButton->setEnabled(false); clearPB->setEnabled(false); } else { - dynSearchActive = true; + m_dynSearchActive = true; // nextButton->setEnabled(true); // prevButton->setEnabled(true); clearPB->setEnabled(true); @@ -275,11 +276,11 @@ void Preview::doSearch(const QString &_text, bool next, bool reverse, } if (found) { - canBeep = true; + m_canBeep = true; } else { - if (canBeep) + if (m_canBeep) QApplication::beep(); - canBeep = false; + m_canBeep = false; } LOGDEB(("Preview::doSearch: return\n")); } @@ -298,7 +299,7 @@ void Preview::prevPressed() void Preview::currentChanged(QWidget * tw) { QWidget *edit = (QWidget *)tw->child("pvEdit"); - currentW = tw; + m_currentW = tw; LOGDEB1(("Preview::currentChanged(). Editor: %p\n", edit)); if (edit == 0) { @@ -331,9 +332,9 @@ void Preview::currentChanged(QWidget * tw) void Preview::selecChanged() { LOGDEB1(("Selection changed\n")); - if (!currentW) + if (!m_currentW) return; - QTextEdit *edit = (QTextEdit *)currentW->child("pvEdit"); + QTextEdit *edit = (QTextEdit *)m_currentW->child("pvEdit"); if (edit == 0) { LOGERR(("Editor child not found\n")); return; @@ -357,9 +358,9 @@ void Preview::selecChanged(){} void Preview::textDoubleClicked(int, int) { LOGDEB2(("Preview::textDoubleClicked\n")); - if (!currentW) + if (!m_currentW) return; - QTextEdit *edit = (QTextEdit *)currentW->child("pvEdit"); + QTextEdit *edit = (QTextEdit *)m_currentW->child("pvEdit"); if (edit == 0) { LOGERR(("Editor child not found\n")); return; @@ -381,10 +382,10 @@ void Preview::closeCurrentTab() return; pvTab->removePage(tw); // Have to remove from tab data list - for (list::iterator it = tabData.begin(); - it != tabData.end(); it++) { + for (list::iterator it = m_tabData.begin(); + it != m_tabData.end(); it++) { if (it->w == tw) { - tabData.erase(it); + m_tabData.erase(it); return; } } @@ -393,7 +394,6 @@ void Preview::closeCurrentTab() } } - QTextEdit *Preview::addEditorTab() { QWidget *anon = new QWidget((QWidget *)pvTab); @@ -404,7 +404,7 @@ QTextEdit *Preview::addEditorTab() anonLayout->addWidget(editor); pvTab->addTab(anon, "Tab"); pvTab->showPage(anon); - tabData.push_back(TabData(anon)); + m_tabData.push_back(TabData(anon)); return editor; } @@ -440,8 +440,8 @@ void Preview::setCurTabProps(const string &fn, const Rcl::Doc &doc, tiptxt += meta_it->second + "\n"; pvTab->setTabToolTip(w,QString::fromUtf8(tiptxt.c_str(), tiptxt.length())); - for (list::iterator it = tabData.begin(); - it != tabData.end(); it++) { + for (list::iterator it = m_tabData.begin(); + it != m_tabData.end(); it++) { if (it->w == w) { it->fn = fn; it->ipath = doc.ipath; @@ -456,8 +456,8 @@ TabData *Preview::tabDataForCurrent() QWidget *w = pvTab->currentPage(); if (w == 0) return 0; - for (list::iterator it = tabData.begin(); - it != tabData.end(); it++) { + for (list::iterator it = m_tabData.begin(); + it != m_tabData.end(); it++) { if (it->w == w) { return &(*it); } @@ -465,11 +465,12 @@ TabData *Preview::tabDataForCurrent() return 0; } -bool Preview::makeDocCurrent(const string &fn, const Rcl::Doc &doc) +bool Preview::makeDocCurrent(const string &fn, size_t sz, + const Rcl::Doc& doc, int docnum, bool sametab) { - LOGDEB(("Preview::makeFileCurrent: %s\n", fn.c_str())); - for (list::iterator it = tabData.begin(); - it != tabData.end(); it++) { + LOGDEB(("Preview::makeDocCurrent: %s\n", fn.c_str())); + for (list::iterator it = m_tabData.begin(); + it != m_tabData.end(); it++) { LOGDEB2(("Preview::makeFileCurrent: compare to w %p, file %s\n", it->w, it->fn.c_str())); if (!it->fn.compare(fn) && !it->ipath.compare(doc.ipath)) { @@ -477,7 +478,16 @@ bool Preview::makeDocCurrent(const string &fn, const Rcl::Doc &doc) return true; } } - return false; + // if just created the first tab was created during init + if (!sametab && !m_justCreated && !addEditorTab()) { + return false; + } + m_justCreated = false; + if (!loadFileInCurrentTab(fn, sz, doc, docnum)) { + closeCurrentTab(); + return false; + } + return true; } /* @@ -779,7 +789,7 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc, progress.close(); if (searchTextLine->text().length() != 0) { - canBeep = true; + m_canBeep = true; doSearch(searchTextLine->text(), true, false); } else { if (hasAnchors) { @@ -790,12 +800,12 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc, // unusable (plus it does not always work) #if (QT_VERSION < 0x040000) #ifdef QT_SCROLL_TO_ANCHOR_BUG - bool ocanbeep = canBeep; - canBeep = false; + bool ocanbeep = m_canBeep; + m_canBeep = false; QString empty; // doSearch(_text, next, reverse, wordOnly) doSearch(empty, true, false, false); - canBeep = ocanbeep; + m_canBeep = ocanbeep; #endif #endif // (QT_VERSION < 0x040000) } diff --git a/src/qtgui/preview_w.h b/src/qtgui/preview_w.h index f37b793d..48bf6c2e 100644 --- a/src/qtgui/preview_w.h +++ b/src/qtgui/preview_w.h @@ -1,6 +1,6 @@ #ifndef _PREVIEW_W_H_INCLUDED_ #define _PREVIEW_W_H_INCLUDED_ -/* @(#$Id: preview_w.h,v 1.12 2007-07-13 06:31:30 dockes Exp $ (C) 2006 J.F.Dockes */ +/* @(#$Id: preview_w.h,v 1.13 2007-07-20 10:55:05 dockes Exp $ (C) 2006 J.F.Dockes */ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -65,39 +65,34 @@ class Preview : public DummyPreviewBase Q_OBJECT public: - Preview(QWidget* parent = 0) - : DummyPreviewBase(parent) {init();} - - ~Preview(){} - - virtual void setSId(int sid, const HiliteData& hdata) + Preview(int sid, // Search Id + const HiliteData& hdata) // Search terms etc. for highlighting + : DummyPreviewBase(0) { + init(); m_searchId = sid; m_hData = hdata; } - virtual void closeEvent( QCloseEvent *e ); - virtual bool eventFilter( QObject *target, QEvent *event ); - virtual bool makeDocCurrent( const string & fn, const Rcl::Doc & doc ); - virtual QTextEdit *getCurrentEditor(); - virtual QTextEdit *addEditorTab(); - virtual bool loadFileInCurrentTab(string fn, size_t sz, - const Rcl::Doc& idoc, int dnm); + + ~Preview(){} + + virtual void closeEvent(QCloseEvent *e ); + virtual bool eventFilter(QObject *target, QEvent *event ); + virtual bool makeDocCurrent(const string &fn, size_t sz, + const Rcl::Doc& idoc, int docnum, + bool sametab = false); public slots: - virtual void searchTextLine_textChanged( const QString & text ); - virtual void doSearch(const QString &str, bool next, bool reverse, - bool wo = false); + virtual void searchTextLine_textChanged(const QString& text); + virtual void doSearch(const QString& str, bool next, bool reverse, + bool wo = false); virtual void nextPressed(); virtual void prevPressed(); - virtual void currentChanged( QWidget * tw ); + virtual void currentChanged(QWidget *tw); virtual void closeCurrentTab(); - virtual void setCurTabProps(const string & fn, const Rcl::Doc & doc, - int docnum); virtual void textDoubleClicked(int, int); - virtual void selecChanged(); - signals: void previewClosed(QWidget *); void wordSelect(QString); @@ -106,18 +101,26 @@ signals: void previewExposed(int sid, int docnum); private: - int m_searchId; // Identifier of search in main window. This is so that - // we make sense when requesting the next document when - // browsing successive search results in a tab. - int matchIndex; - int matchPara; - bool dynSearchActive; - bool canBeep; - bool m_loading; - list tabData; - QWidget *currentW; - HiliteData m_hData; + // Identifier of search in main window. This is used to check that + // we make sense when requesting the next document when browsing + // successive search results in a tab. + int m_searchId; + + bool m_dynSearchActive; + bool m_canBeep; + bool m_loading; + list m_tabData; + QWidget *m_currentW; + HiliteData m_hData; + bool m_justCreated; // First tab create is different + void init(); + virtual void setCurTabProps(const string& fn, const Rcl::Doc& doc, + int docnum); + virtual QTextEdit *getCurrentEditor(); + virtual QTextEdit *addEditorTab(); + virtual bool loadFileInCurrentTab(string fn, size_t sz, + const Rcl::Doc& idoc, int dnm); TabData *tabDataForCurrent(); // Return auxiliary data pointer for cur tab }; diff --git a/src/qtgui/rclmain_w.cpp b/src/qtgui/rclmain_w.cpp index b1dc31aa..520e7538 100644 --- a/src/qtgui/rclmain_w.cpp +++ b/src/qtgui/rclmain_w.cpp @@ -1,5 +1,5 @@ #ifndef lint -static char rcsid[] = "@(#$Id: rclmain_w.cpp,v 1.32 2007-07-13 06:31:30 dockes Exp $ (C) 2005 J.F.Dockes"; +static char rcsid[] = "@(#$Id: rclmain_w.cpp,v 1.33 2007-07-20 10:55:05 dockes Exp $ (C) 2005 J.F.Dockes"; #endif /* * This program is free software; you can redistribute it and/or modify @@ -458,7 +458,9 @@ void RclMain::startPreview(int docnum, int mod) curPreview = 0; } if (curPreview == 0) { - curPreview = new Preview(0); + HiliteData hdata; + m_searchData->getTerms(hdata.terms, hdata.groups, hdata.gslks); + curPreview = new Preview(m_searchId, hdata); if (curPreview == 0) { QMessageBox::warning(0, tr("Warning"), tr("Can't create preview window"), @@ -466,10 +468,6 @@ void RclMain::startPreview(int docnum, int mod) QMessageBox::NoButton); return; } - HiliteData hdata; - m_searchData->getTerms(hdata.terms, hdata.groups, hdata.gslks); - curPreview->setSId(m_searchId, hdata); - curPreview->setCaption(resList->getDescription()); connect(curPreview, SIGNAL(previewClosed(QWidget *)), this, SLOT(previewClosed(QWidget *))); connect(curPreview, SIGNAL(wordSelect(QString)), @@ -480,16 +478,10 @@ void RclMain::startPreview(int docnum, int mod) this, SLOT(previewPrevInTab(int, int))); connect(curPreview, SIGNAL(previewExposed(int, int)), this, SLOT(previewExposed(int, int))); + curPreview->setCaption(resList->getDescription()); curPreview->show(); - } else { - if (curPreview->makeDocCurrent(fn, doc)) { - // Already there - return; - } - (void)curPreview->addEditorTab(); - } - if (!curPreview->loadFileInCurrentTab(fn, st.st_size, doc, docnum)) - curPreview->closeCurrentTab(); + } + curPreview->makeDocCurrent(fn, st.st_size, doc, docnum); } /** @@ -509,7 +501,7 @@ void RclMain::startPreview(Rcl::Doc doc) fn.c_str()); return; } - Preview *preview = new Preview(0); + Preview *preview = new Preview(0, HiliteData()); if (preview == 0) { QMessageBox::warning(0, tr("Warning"), tr("Can't create preview window"), @@ -517,12 +509,10 @@ void RclMain::startPreview(Rcl::Doc doc) QMessageBox::NoButton); return; } - preview->setSId(0, HiliteData()); connect(preview, SIGNAL(wordSelect(QString)), this, SLOT(ssearchAddTerm(QString))); preview->show(); - if (!preview->loadFileInCurrentTab(fn, st.st_size, doc, 0)) - preview->closeCurrentTab(); + preview->makeDocCurrent(fn, st.st_size, doc, 0); } // Show next document from result list in current preview tab @@ -531,6 +521,13 @@ void RclMain::previewNextInTab(int sid, int docnum) LOGDEB(("RclMain::previewNextInTab sid %d docnum %d, m_sid %d\n", sid, docnum, m_searchId)); + // We should handle this case better: this happens when the latest + // preview was closed and the user asks for the next document in + // an older one. The whole situation with multiple previews and + // showNext/showPrev is a mess, just avoid crashing for now. + if (curPreview == 0) + return; + if (sid != m_searchId) { QMessageBox::warning(0, "Recoll", tr("This search is not active any more")); @@ -560,8 +557,7 @@ void RclMain::previewNextInTab(int sid, int docnum) return; } - if (!curPreview->loadFileInCurrentTab(fn, st.st_size, doc, docnum)) - curPreview->closeCurrentTab(); + curPreview->makeDocCurrent(fn, st.st_size, doc, docnum, true); } // Show previous document from result list in current preview tab @@ -570,6 +566,13 @@ void RclMain::previewPrevInTab(int sid, int docnum) LOGDEB(("RclMain::previewPrevInTab sid %d docnum %d, m_sid %d\n", sid, docnum, m_searchId)); + // We should handle this case better: this happens when the latest + // preview was closed and the user asks for the next document in + // an older one. The whole situation with multiple previews and + // showNext/showPrev is a mess, just avoid crashing for now. + if (curPreview == 0) + return; + if (sid != m_searchId) { QMessageBox::warning(0, "Recoll", tr("This search is not active any more")); @@ -596,8 +599,7 @@ void RclMain::previewPrevInTab(int sid, int docnum) fn.c_str()); return; } - if (!curPreview->loadFileInCurrentTab(fn, st.st_size, doc, docnum)) - curPreview->closeCurrentTab(); + curPreview->makeDocCurrent(fn, st.st_size, doc, docnum, true); } // Preview tab exposed: possibly tell reslist (to color the paragraph)