From 199abb998014630a1bee0a427b7ad79909ab92d1 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Thu, 10 Sep 2020 10:53:38 +0200 Subject: [PATCH] Get the result table detail area to use the reslist HTML style (e.g. for dark mode) --- src/qtgui/restable.cpp | 144 ++++++++++++++++++--------------- src/qtgui/restable.h | 3 +- src/query/reslistpager.cpp | 29 +++++++ src/query/reslistpager.h | 10 +++ src/sampleconf/recoll-dark.qss | 8 +- 5 files changed, 125 insertions(+), 69 deletions(-) diff --git a/src/qtgui/restable.cpp b/src/qtgui/restable.cpp index 44a58f36..289b81c7 100644 --- a/src/qtgui/restable.cpp +++ b/src/qtgui/restable.cpp @@ -79,22 +79,28 @@ public: ResTablePager(ResTable *p) : ResListPager(1, prefs.alwaysSnippets), m_parent(p) {} - virtual bool append(const string& data, int idx, const Rcl::Doc& doc); - virtual string trans(const string& in); - virtual const string &parFormat(); - virtual string absSep() {return (const char *)(prefs.abssep.toUtf8());} + virtual bool append(const string& data) override; + virtual bool flush() override; + virtual string trans(const string& in) override; + virtual const string &parFormat() override; + virtual string absSep() override { + return (const char *)(prefs.abssep.toUtf8());} + virtual string headerContent() override { + return qs2utf8s(prefs.reslistheadertext);} private: ResTable *m_parent; + string m_data; }; -bool ResTablePager::append(const string& data, int, const Rcl::Doc&) +bool ResTablePager::append(const string& data) { - m_parent->m_detail->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor); - m_parent->m_detail->textCursor().insertBlock(); - m_parent->m_detail->insertHtml(u8s2qs(data)); - -// LOGDEB("RESTABLEPAGER::APPEND: data : " << data << std::endl); -// m_parent->m_detail->setHtml(u8s2qs(data)); + m_data += data; + return true; +} +bool ResTablePager::flush() +{ + m_parent->m_detail->setHtml(u8s2qs(m_data)); + m_data = ""; return true; } @@ -147,6 +153,12 @@ void ResTableDetailArea::setFont() } } +void ResTableDetailArea::init() +{ + setFont(); + QTextBrowser::setHtml(""); +} + ////////////////////////////////////////////////////////////////////////////// //// Data model methods //// @@ -474,51 +486,54 @@ public: QStyleOptionViewItem opt = option; initStyleOption(&opt, index); QVariant value = index.data(Qt::DisplayRole); + QString text; if (value.isValid() && !value.isNull()) { - QString text = value.toString(); - if (!text.isEmpty()) { - QTextDocument document; - painter->save(); - QString fstyle; - if (prefs.reslistfontsize > 0) { - int fs = prefs.reslistfontsize <= fsadjusttable ? - prefs.reslistfontsize : - prefs.reslistfontsize - fsadjusttable; - fstyle = QString("font-size: %1pt").arg(fs); - } - if (opt.state & QStyle::State_Selected) { - painter->fillRect(opt.rect, opt.palette.highlight()); - // Set the foreground color. Tried with pen - // approach did not seem to work, probably it's - // reset by the textdocument. Couldn't use - // setdefaultstylesheet() either. the div thing is - // an ugly hack. Works for now - QString ntxt("
"; - ntxt += text + QString::fromUtf8("
"); - text.swap(ntxt); - } else { - if (!fstyle.isEmpty()) { - QString ntxt("
") + text + QString("
"); - text.swap(ntxt); - } - } - painter->setClipRect(option.rect); - QPoint where = option.rect.topLeft(); - where.ry() += TEXTINCELLVTRANS; - painter->translate(where); - document.setHtml(text); - document.drawContents(painter); - painter->restore(); - return; - } + text = value.toString(); } - QStyledItemDelegate::paint(painter, option, index); + if (text.isEmpty()) { + QStyledItemDelegate::paint(painter, option, index); + return; + } + + painter->save(); + + QTextDocument document; + QString fstyle; + if (prefs.reslistfontsize > 0) { + int fs = prefs.reslistfontsize <= fsadjusttable ? + prefs.reslistfontsize : + prefs.reslistfontsize - fsadjusttable; + fstyle = QString("font-size: %1pt").arg(fs); + } + if (opt.state & QStyle::State_Selected) { + painter->fillRect(opt.rect, opt.palette.highlight()); + // Set the foreground color. Tried with pen + // approach did not seem to work, probably it's + // reset by the textdocument. Couldn't use + // setdefaultstylesheet() either. the div thing is + // an ugly hack. Works for now + QString ntxt("
"; + ntxt += text + QString::fromUtf8("
"); + text.swap(ntxt); + } else { + if (!fstyle.isEmpty()) { + QString ntxt("
") + text + QString("
"); + text.swap(ntxt); + } + } + painter->setClipRect(option.rect); + QPoint where = option.rect.topLeft(); + where.ry() += TEXTINCELLVTRANS; + painter->translate(where); + document.setHtml(text); + document.drawContents(painter); + painter->restore(); } }; @@ -594,7 +609,7 @@ void ResTable::init() m_detail->setReadOnly(true); m_detail->setUndoRedoEnabled(false); m_detail->setOpenLinks(false); - m_detail->setFont(); + m_detail->init(); // signals and slots connections connect(m_detail, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(linkWasClicked(const QUrl &))); @@ -652,8 +667,9 @@ void ResTable::setRclMain(RclMain *m, bool ismain) void ResTable::onUiPrefsChanged() { - if (m_detail) - m_detail->setFont(); + if (m_detail) { + m_detail->init(); + } } int ResTable::getDetailDocNumOrTopRow() @@ -670,7 +686,7 @@ void ResTable::makeRowVisible(int row) QModelIndex modelIndex = m_model->index(row, 0); tableView->scrollTo(modelIndex, QAbstractItemView::PositionAtTop); tableView->selectionModel()->clear(); - m_detail->clear(); + m_detail->init(); m_detaildocnum = -1; } @@ -715,11 +731,11 @@ void ResTable::onTableView_currentChanged(const QModelIndex& index) return; Rcl::Doc doc; if (m_model->getDocSource()->getDoc(index.row(), doc)) { - m_detail->clear(); + m_detail->init(); m_detaildocnum = index.row(); m_detaildoc = doc; - m_pager->displayDoc(theconfig, m_detaildocnum, m_detaildoc, - m_model->m_hdata); + m_pager->displaySingleDoc(theconfig, m_detaildocnum, m_detaildoc, + m_model->m_hdata); emit(detailDocChanged(doc, m_model->getDocSource())); } else { m_detaildocnum = -1; @@ -748,7 +764,7 @@ void ResTable::setDocSource(std::shared_ptr nsource) if (m_pager) m_pager->setDocSource(nsource, 0); if (m_detail) - m_detail->clear(); + m_detail->init(); m_detaildocnum = -1; } @@ -828,7 +844,7 @@ void ResTable::readDocSource(bool resetPos) m_model->m_hdata.clear(); } m_model->readDocSource(); - m_detail->clear(); + m_detail->init(); m_detaildocnum = -1; } @@ -916,7 +932,7 @@ void ResTable::onDoubleClick(const QModelIndex& index) Rcl::Doc doc; if (m_model->getDocSource()->getDoc(index.row(), doc)) { if (m_detaildocnum != index.row()) { - m_detail->clear(); + m_detail->init(); m_detaildocnum = index.row(); m_pager->displayDoc(theconfig, index.row(), m_detaildoc, m_model->m_hdata); diff --git a/src/qtgui/restable.h b/src/qtgui/restable.h index 2623bff8..087e5dc4 100644 --- a/src/qtgui/restable.h +++ b/src/qtgui/restable.h @@ -96,7 +96,8 @@ class ResTableDetailArea : public QTextBrowser { public slots: virtual void createPopupMenu(const QPoint& pos); virtual void setFont(); - + virtual void init(); + private: ResTable *m_table; }; diff --git a/src/query/reslistpager.cpp b/src/query/reslistpager.cpp index ce0f18d7..1c63d625 100644 --- a/src/query/reslistpager.cpp +++ b/src/query/reslistpager.cpp @@ -462,8 +462,37 @@ void ResListPager::displayPage(RclConfig *config) chunk << "

" << endl; chunk << "" << endl; append(chunk.rdbuf()->str()); + flush(); } +void ResListPager::displaySingleDoc(RclConfig *config, int idx, + Rcl::Doc& doc, + const HighlightData& hdata) +{ + ostringstream chunk; + + // Header + // Note: have to append text in chunks that make sense + // html-wise. If we break things up too much, the editor + // gets confused. + string bdtag("\n" + << "\n" + << headerContent() + << "\n" << bdtag << "\n"; + append(chunk.rdbuf()->str()); + // Document + displayDoc(config, idx, doc, hdata, string()); + // Footer + append("\n"); + flush(); +} + + // Default implementations for things that should be implemented by // specializations string ResListPager::nextUrl() diff --git a/src/query/reslistpager.h b/src/query/reslistpager.h index 8bc30320..b03dc1db 100644 --- a/src/query/reslistpager.h +++ b/src/query/reslistpager.h @@ -79,9 +79,16 @@ public: } void resultPageNext(); void resultPageFor(int docnum); + + /* Display page of results */ void displayPage(RclConfig *); + /* Display page with single document */ + void displaySingleDoc(RclConfig *config, int idx, + Rcl::Doc& doc, const HighlightData& hdata); + /* Generate HTML for single document inside page */ void displayDoc(RclConfig *, int idx, Rcl::Doc& doc, const HighlightData& hdata, const string& sh = ""); + bool pageEmpty() {return m_respage.size() == 0;} string queryDescription() { @@ -95,6 +102,9 @@ public: virtual bool append(const string& data, int, const Rcl::Doc&) { return append(data); } + /* Implementing this allows accumulating the text and setting the HTML + at once */ + virtual bool flush() {return true;} // Translation function. This is reimplemented in the qt reslist // object For this to work, the strings must be duplicated inside // reslist.cpp (see the QT_TR_NOOP in there). Very very unwieldy. diff --git a/src/sampleconf/recoll-dark.qss b/src/sampleconf/recoll-dark.qss index 69e6b4c7..2604ade5 100644 --- a/src/sampleconf/recoll-dark.qss +++ b/src/sampleconf/recoll-dark.qss @@ -29,7 +29,6 @@ /* Light on dark text everywhere*/ * { -/* font-size: 14pt;*/ background-color: #373737; color: #ffffff; selection-background-color: #424e72; @@ -61,9 +60,10 @@ QTabWidget QTabBar::tab:selected { /* * Slightly *darker* background for text entry areas, and brighter * (lighter) foreground. + * QTableView excluded until we fix the font color... */ QComboBox[editable="true"], QTextEdit, QLineEdit, -QTextBrowser, QTableView, QWebView, QPlainTextEdit { - background-color: #101010; /*Changed*/ - color: #ffffff; /*Changed*/ +QTextBrowser, QWebView, QPlainTextEdit { + background-color: #101010; + color: #ffffff; }