GUI preview window: use standard qt tools to simplify the code

This commit is contained in:
Jean-Francois Dockes 2018-12-02 13:32:48 +01:00
parent 254131baaa
commit 5bb0b9f4a5
4 changed files with 177 additions and 126 deletions

104
src/qtgui/preview.ui Normal file
View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Preview</class>
<widget class="QWidget" name="Preview">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>611</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="pvTab">
<property name="currentIndex">
<number>0</number>
</property>
<property name="tabsClosable">
<bool>true</bool>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="searchLBL">
<property name="text">
<string>&amp;Search for:</string>
</property>
<property name="buddy">
<cstring>searchTextCMB</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="searchTextCMB">
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="nextPB">
<property name="text">
<string>&amp;Next</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="prevPB">
<property name="text">
<string>&amp;Previous</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearPB">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="casematchCB">
<property name="text">
<string>Match &amp;Case</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -69,73 +69,27 @@ static const QKeySequence printTabKS(Qt::ControlModifier+Qt::Key_P);
void Preview::init()
{
setObjectName("Preview");
QVBoxLayout* previewLayout = new QVBoxLayout(this);
LOGDEB("Preview::init\n");
// Create the first tab (the tab widget is created with one
// initial tab for ease of use in designer, we remove it).
addEditorTab();
pvTab->removeTab(0);
pvTab = new QTabWidget(this);
// Create the first tab. Should be possible to use addEditorTab
// but this causes a pb with the sizeing
QWidget *unnamed = new QWidget(pvTab);
QVBoxLayout *unnamedLayout = new QVBoxLayout(unnamed);
PreviewTextEdit *pvEdit = new PreviewTextEdit(unnamed, "pvEdit", this);
pvEdit->setReadOnly(true);
pvEdit->setUndoRedoEnabled(false);
unnamedLayout->addWidget(pvEdit);
pvTab->addTab(unnamed, "");
previewLayout->addWidget(pvTab);
// Create the buttons and entry field
QHBoxLayout *layout3 = new QHBoxLayout(0);
searchLabel = new QLabel(this);
layout3->addWidget(searchLabel);
searchTextCMB = new QComboBox(this);
searchTextCMB->setEditable(true);
searchTextCMB->setInsertPolicy(QComboBox::NoInsert);
searchTextCMB->setDuplicatesEnabled(false);
for (unsigned int i = 0; i < m_hData.ugroups.size(); i++) {
for (const auto& ugroup : m_hData.ugroups) {
QString s;
for (unsigned int j = 0; j < m_hData.ugroups[i].size(); j++) {
s.append(QString::fromUtf8(m_hData.ugroups[i][j].c_str()));
if (j != m_hData.ugroups[i].size()-1)
s.append(" ");
for (const auto& elt : ugroup) {
s.append(u8s2qs(elt));
}
s = s.trimmed();
searchTextCMB->addItem(s);
}
searchTextCMB->setEditText("");
searchTextCMB->setCompleter(0);
layout3->addWidget(searchTextCMB);
nextButton = new QPushButton(this);
nextButton->setEnabled(true);
layout3->addWidget(nextButton);
prevButton = new QPushButton(this);
prevButton->setEnabled(true);
layout3->addWidget(prevButton);
clearPB = new QPushButton(this);
clearPB->setEnabled(false);
layout3->addWidget(clearPB);
matchCheck = new QCheckBox(this);
layout3->addWidget(matchCheck);
previewLayout->addLayout(layout3);
resize(QSize(640, 480).expandedTo(minimumSizeHint()));
// buddies
searchLabel->setBuddy(searchTextCMB);
searchLabel->setText(tr("&Search for:"));
nextButton->setText(tr("&Next"));
prevButton->setText(tr("&Previous"));
clearPB->setText(tr("Clear"));
matchCheck->setText(tr("Match &Case"));
QPushButton * bt = new QPushButton(tr("Close Tab"), this);
pvTab->setCornerWidget(bt);
if (prefs.pvwidth > 100) {
resize(prefs.pvwidth, prefs.pvheight);
} else {
resize(QSize(640, 480).expandedTo(minimumSizeHint()));
}
(void)new HelpClient(this);
HelpClient::installMap((const char *)objectName().toUtf8(),
@ -146,12 +100,11 @@ void Preview::init()
this, SLOT(searchTextFromIndex(int)));
connect(searchTextCMB, SIGNAL(editTextChanged(const QString&)),
this, SLOT(searchTextChanged(const QString&)));
connect(nextButton, SIGNAL(clicked()), this, SLOT(nextPressed()));
connect(prevButton, SIGNAL(clicked()), this, SLOT(prevPressed()));
connect(nextPB, SIGNAL(clicked()), this, SLOT(nextPressed()));
connect(prevPB, SIGNAL(clicked()), this, SLOT(prevPressed()));
connect(clearPB, SIGNAL(clicked()), searchTextCMB, SLOT(clearEditText()));
connect(pvTab, SIGNAL(currentChanged(int)),
this, SLOT(currentChanged(int)));
connect(bt, SIGNAL(clicked()), this, SLOT(closeCurrentTab()));
connect(pvTab, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
connect(pvTab, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
connect(new QShortcut(closeKS, this), SIGNAL (activated()),
this, SLOT (close()));
@ -164,9 +117,6 @@ void Preview::init()
connect(new QShortcut(printTabKS, this), SIGNAL (activated()),
this, SIGNAL (printCurrentPreviewRequest()));
if (prefs.pvwidth > 100) {
resize(prefs.pvwidth, prefs.pvheight);
}
currentChanged(pvTab->currentIndex());
}
@ -203,13 +153,9 @@ void Preview::closeEvent(QCloseEvent *e)
/* Release all temporary files (but maybe none is actually set) */
for (int i = 0; i < pvTab->count(); i++) {
QWidget *tw = pvTab->widget(i);
if (tw) {
PreviewTextEdit *edit =
tw->findChild<PreviewTextEdit*>("pvEdit");
if (edit) {
forgetTempFile(edit->m_tmpfilename);
}
PreviewTextEdit *edit = editor(i);
if (edit) {
forgetTempFile(edit->m_tmpfilename);
}
}
emit previewExposed(this, m_searchId, -1);
@ -295,17 +241,6 @@ void Preview::searchTextFromIndex(int idx)
m_searchTextFromIndex = idx;
}
PreviewTextEdit *Preview::currentEditor()
{
LOGDEB2("Preview::currentEditor()\n");
QWidget *tw = pvTab->currentWidget();
PreviewTextEdit *edit = 0;
if (tw) {
edit = tw->findChild<PreviewTextEdit*>("pvEdit");
}
return edit;
}
// Save current document to file
void Preview::emitSaveDocToFile()
{
@ -327,7 +262,7 @@ void Preview::doSearch(const QString &_text, bool next, bool reverse,
" word " << wordOnly << "\n");
QString text = _text;
bool matchCase = matchCheck->isChecked();
bool matchCase = casematchCB->isChecked();
PreviewTextEdit *edit = currentEditor();
if (edit == 0) {
// ??
@ -419,9 +354,7 @@ void Preview::prevPressed()
void Preview::currentChanged(int index)
{
LOGDEB2("PreviewTextEdit::currentChanged\n");
QWidget *tw = pvTab->widget(index);
PreviewTextEdit *edit =
tw->findChild<PreviewTextEdit*>("pvEdit");
PreviewTextEdit *edit = editor(index);
LOGDEB1("Preview::currentChanged(). Editor: " << edit << "\n");
if (edit == 0) {
@ -430,7 +363,7 @@ void Preview::currentChanged(int index)
}
edit->setFocus();
// Disconnect the print signal and reconnect it to the current editor
LOGDEB("Disconnecting reconnecting print signal\n");
LOGDEB1("Disconnecting reconnecting print signal\n");
disconnect(this, SIGNAL(printCurrentPreviewRequest()), 0, 0);
connect(this, SIGNAL(printCurrentPreviewRequest()), edit, SLOT(print()));
edit->installEventFilter(this);
@ -446,27 +379,45 @@ void Preview::closeCurrentTab()
CancelCheck::instance().setCancel();
return;
}
PreviewTextEdit *e = currentEditor();
if (e)
forgetTempFile(e->m_tmpfilename);
closeTab(pvTab->currentIndex());
}
void Preview::closeTab(int index)
{
LOGDEB1("Preview::closeTab: m_loading " << m_loading << "\n");
if (m_loading) {
CancelCheck::instance().setCancel();
return;
}
PreviewTextEdit *edit = editor(index);
if (edit)
forgetTempFile(edit->m_tmpfilename);
if (pvTab->count() > 1) {
pvTab->removeTab(pvTab->currentIndex());
pvTab->removeTab(index);
} else {
close();
}
}
PreviewTextEdit *Preview::editor(int index)
{
return dynamic_cast<PreviewTextEdit*>(pvTab->widget(index));
}
PreviewTextEdit *Preview::currentEditor()
{
LOGDEB2("Preview::currentEditor()\n");
return editor(pvTab->currentIndex());
}
PreviewTextEdit *Preview::addEditorTab()
{
LOGDEB1("PreviewTextEdit::addEditorTab()\n");
QWidget *anon = new QWidget((QWidget *)pvTab);
QVBoxLayout *anonLayout = new QVBoxLayout(anon);
PreviewTextEdit *editor = new PreviewTextEdit(anon, "pvEdit", this);
PreviewTextEdit *editor = new PreviewTextEdit(pvTab, "pvEdit", this);
editor->setReadOnly(true);
editor->setUndoRedoEnabled(false );
anonLayout->addWidget(editor);
pvTab->addTab(anon, "Tab");
pvTab->setCurrentIndex(pvTab->count() -1);
pvTab->addTab(editor, "Tab");
pvTab->setCurrentIndex(pvTab->count() - 1);
return editor;
}
@ -523,15 +474,11 @@ bool Preview::makeDocCurrent(const Rcl::Doc& doc, int docnum, bool sametab)
/* Check if we already have this page */
for (int i = 0; i < pvTab->count(); i++) {
QWidget *tw = pvTab->widget(i);
if (tw) {
PreviewTextEdit *edit =
tw->findChild<PreviewTextEdit*>("pvEdit");
if (edit && !edit->m_url.compare(doc.url) &&
!edit->m_ipath.compare(doc.ipath)) {
pvTab->setCurrentIndex(i);
return true;
}
PreviewTextEdit *edit = editor(i);
if (edit && !edit->m_url.compare(doc.url) &&
!edit->m_ipath.compare(doc.ipath)) {
pvTab->setCurrentIndex(i);
return true;
}
}
@ -547,6 +494,7 @@ bool Preview::makeDocCurrent(const Rcl::Doc& doc, int docnum, bool sametab)
raise();
return true;
}
void Preview::togglePlainPre()
{
switch (prefs.previewPlainPre) {

View File

@ -45,6 +45,8 @@
#include "plaintorich.h"
#include "rclmain_w.h"
#include "ui_preview.h"
class QTabWidget;
class QLabel;
class QPushButton;
@ -106,13 +108,15 @@ private:
};
class Preview : public QWidget {
Q_OBJECT;
class Preview : public QWidget, public Ui::Preview {
Q_OBJECT
public:
Preview(RclMain *m, int sid, // Search Id
const HighlightData& hdata) // Search terms etc. for highlighting
: QWidget(0), m_rclmain(m), m_searchId(sid), m_hData(hdata) {
: m_rclmain(m), m_searchId(sid), m_hData(hdata) {
setupUi(this);
init();
}
@ -142,6 +146,7 @@ public slots:
// Tabs management
virtual void currentChanged(int);
virtual void closeCurrentTab();
virtual void closeTab(int index);
virtual void emitShowNext();
virtual void emitShowPrev();
@ -158,7 +163,7 @@ signals:
void saveDocToFile(Rcl::Doc);
private:
RclMain *m_rclmain{0};
RclMain *m_rclmain;
// 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.
@ -173,16 +178,9 @@ private:
HighlightData m_hData;
bool m_justCreated{true}; // First tab create is different
QTabWidget* pvTab{0};
QLabel* searchLabel{0};
QComboBox *searchTextCMB{0};
QPushButton* nextButton{0};
QPushButton* prevButton{0};
QPushButton* clearPB{0};
QCheckBox* matchCheck{0};
void init();
virtual void setCurTabProps(const Rcl::Doc& doc, int docnum);
virtual PreviewTextEdit *editor(int);
virtual PreviewTextEdit *currentEditor();
virtual PreviewTextEdit *addEditorTab();
virtual bool loadDocInCurrentTab(const Rcl::Doc& idoc, int dnm);

View File

@ -91,21 +91,22 @@ SOURCES += \
FORMS = \
advsearch.ui \
crontool.ui \
widgets/editdialog.ui \
firstidx.ui \
idxsched.ui \
widgets/listdialog.ui \
preview.ui \
ptrans.ui \
rclmain.ui \
restable.ui \
rtitool.ui \
snippets.ui \
specialindex.ui \
spell.ui \
snippets.ui \
ssearchb.ui \
uiprefs.ui \
viewaction.ui \
webcache.ui
webcache.ui \
widgets/editdialog.ui \
widgets/listdialog.ui
RESOURCES = recoll.qrc