GUI: result table. Add shortcuts Ctrl+[a-z] to make any of the 1st 26 rows current. Add method and shortcut to copy the current result text to the clipboard.

This commit is contained in:
Jean-Francois Dockes 2021-01-27 09:58:06 +01:00
parent cced5efb45
commit 72f17a73d3
2 changed files with 95 additions and 43 deletions

View File

@ -40,6 +40,7 @@
#include <QMessageBox> #include <QMessageBox>
#include <QTimer> #include <QTimer>
#include <QKeyEvent> #include <QKeyEvent>
#include <QClipboard>
#include "recoll.h" #include "recoll.h"
#include "docseq.h" #include "docseq.h"
@ -360,7 +361,11 @@ QVariant RecollModel::headerData(int idx, Qt::Orientation orientation,
(orientation == Qt::Vertical ? "vertical":"horizontal") << (orientation == Qt::Vertical ? "vertical":"horizontal") <<
" role " << role << "\n"); " role " << role << "\n");
if (orientation == Qt::Vertical && role == Qt::DisplayRole) { if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
return idx; if (idx < 26) {
return QString("%1/%2").arg(idx).arg(char('a'+idx));
} else {
return idx;
}
} }
if (orientation == Qt::Horizontal && role == Qt::DisplayRole && if (orientation == Qt::Horizontal && role == Qt::DisplayRole &&
idx < int(m_fields.size())) { idx < int(m_fields.size())) {
@ -568,6 +573,7 @@ void ResTable::setDefRowHeight()
font.setPointSize(fs); font.setPointSize(fs);
QFontMetrics fm(font); QFontMetrics fm(font);
header->setDefaultSectionSize(fm.height() + ROWHEIGHTPAD); header->setDefaultSectionSize(fm.height() + ROWHEIGHTPAD);
header->setSectionResizeMode(QHeaderView::Fixed);
} }
} }
@ -594,18 +600,24 @@ void ResTable::init()
connect(&SCBase::scBase(), SIGNAL(shortcutsChanged()), connect(&SCBase::scBase(), SIGNAL(shortcutsChanged()),
this, SLOT(onNewShortcuts())); this, SLOT(onNewShortcuts()));
new QShortcut(QKeySequence("Ctrl+0"), this, SLOT(setCurrentRow0())); // Set "go to row" accelerator shortcuts. letter or digit for 0-9,
new QShortcut(QKeySequence("Ctrl+1"), this, SLOT(setCurrentRow1())); // then letter up to 25
new QShortcut(QKeySequence("Ctrl+2"), this, SLOT(setCurrentRow2())); std::function<void(int)> setrow =
new QShortcut(QKeySequence("Ctrl+3"), this, SLOT(setCurrentRow3())); std::bind(&ResTable::setCurrentRow, this, std::placeholders::_1);
new QShortcut(QKeySequence("Ctrl+4"), this, SLOT(setCurrentRow4())); for (int i = 0; i <= 25; i++) {
new QShortcut(QKeySequence("Ctrl+5"), this, SLOT(setCurrentRow5())); if (i <= 9) {
new QShortcut(QKeySequence("Ctrl+6"), this, SLOT(setCurrentRow6())); auto qs = QString("Ctrl+%1").arg(i);
new QShortcut(QKeySequence("Ctrl+7"), this, SLOT(setCurrentRow7())); auto sc = new QShortcut(QKeySequence(qs2utf8s(qs).c_str()), this);
new QShortcut(QKeySequence("Ctrl+8"), this, SLOT(setCurrentRow8())); auto lnk = new SCData(this, setrow, i);
new QShortcut(QKeySequence("Ctrl+9"), this, SLOT(setCurrentRow9())); connect(sc, SIGNAL(activated()), lnk, SLOT(activate()));
}
auto qs = QString("Ctrl+Shift+%1").arg(char('a'+i));
auto sc = new QShortcut(QKeySequence(qs2utf8s(qs).c_str()), this);
auto lnk = new SCData(this, setrow, i);
connect(sc, SIGNAL(activated()), lnk, SLOT(activate()));
}
QShortcut *sc = new QShortcut(QKeySequence(Qt::Key_Escape), this); auto sc = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(sc, SIGNAL(activated()), connect(sc, SIGNAL(activated()),
tableView->selectionModel(), SLOT(clear())); tableView->selectionModel(), SLOT(clear()));
@ -640,9 +652,6 @@ void ResTable::init()
#else #else
header->setMovable(true); header->setMovable(true);
#endif #endif
if (prefs.noResTableHeader) {
header->hide();
}
setDefRowHeight(); setDefRowHeight();
connect(tableView->selectionModel(), connect(tableView->selectionModel(),
@ -674,6 +683,7 @@ void ResTable::init()
splitter->setSizes(sizes); splitter->setSizes(sizes);
} }
installEventFilter(this); installEventFilter(this);
onUiPrefsChanged();
} }
void ResTable::onNewShortcuts() void ResTable::onNewShortcuts()
@ -688,6 +698,16 @@ void ResTable::onNewShortcuts()
"Ctrl+E", m_showsnipssc, menuShowSnippets); "Ctrl+E", m_showsnipssc, menuShowSnippets);
SETSHORTCUT(this, tr("Result Table"), tr("Show Header"), SETSHORTCUT(this, tr("Result Table"), tr("Show Header"),
"Ctrl+H", m_showheadersc, toggleHeader); "Ctrl+H", m_showheadersc, toggleHeader);
SETSHORTCUT(this, tr("Result Table"), tr("Show Vertical Header"),
"Ctrl+V", m_showvheadersc, toggleVHeader);
SETSHORTCUT(this, tr("Result Table"), tr("Copy current document text"),
"Ctrl+Shift+[", m_copycurtextsc, copyCurrentRowText);
std::vector<QShortcut*> scps={
m_opensc, m_openquitsc, m_previewsc, m_showsnipssc, m_showheadersc,
m_showvheadersc, m_copycurtextsc};
for (auto& scp : scps) {
scp->setContext(Qt::WidgetWithChildrenShortcut);
}
} }
bool ResTable::eventFilter(QObject* obj, QEvent* event) bool ResTable::eventFilter(QObject* obj, QEvent* event)
@ -746,6 +766,15 @@ void ResTable::toggleHeader()
} }
} }
void ResTable::toggleVHeader()
{
if (tableView->verticalHeader()->isVisible()) {
tableView->verticalHeader()->hide();
} else {
tableView->verticalHeader()->show();
}
}
void ResTable::onUiPrefsChanged() void ResTable::onUiPrefsChanged()
{ {
if (m_detail) { if (m_detail) {
@ -756,26 +785,37 @@ void ResTable::onUiPrefsChanged()
} else { } else {
tableView->horizontalHeader()->show(); tableView->horizontalHeader()->show();
} }
if (prefs.showResTableVHeader) {
tableView->verticalHeader()->show();
} else {
tableView->verticalHeader()->hide();
}
} }
#define SETCURRENTROW(INDEX) \ void ResTable::copyCurrentRowText()
void ResTable::setCurrentRow##INDEX() \ {
{ \ auto index = tableView->selectionModel()->currentIndex();
tableView->setFocus(Qt::ShortcutFocusReason); \ if (!index.isValid()) {
tableView->selectionModel()->setCurrentIndex( \ LOGINF("ResTable::copyCurrentRowText: invalid current index\n");
m_model->index(INDEX, 0), \ return;
QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows); \
} }
SETCURRENTROW(0) int row = index.row();
SETCURRENTROW(1) LOGDEB("copyCurrentRowText: row " << row << "\n");
SETCURRENTROW(2) Rcl::Doc doc;
SETCURRENTROW(3) auto source = m_model->getDocSource();
SETCURRENTROW(4) if (source && source->getDoc(row, doc) && rcldb &&
SETCURRENTROW(5) rcldb->getDocRawText(doc)) {
SETCURRENTROW(6) QApplication::clipboard()->setText(u8s2qs(doc.text));
SETCURRENTROW(7) };
SETCURRENTROW(8) }
SETCURRENTROW(9)
void ResTable::setCurrentRow(int row)
{
tableView->setFocus(Qt::ShortcutFocusReason);
tableView->selectionModel()->setCurrentIndex(
m_model->index(row, 0),
QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows);
}
int ResTable::getDetailDocNumOrTopRow() int ResTable::getDetailDocNumOrTopRow()
{ {

View File

@ -25,6 +25,7 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <fstream> #include <fstream>
#include <functional>
#include "ui_restable.h" #include "ui_restable.h"
#include "docseq.h" #include "docseq.h"
@ -167,17 +168,10 @@ public slots:
virtual void takeFocus(); virtual void takeFocus();
virtual void onUiPrefsChanged(); virtual void onUiPrefsChanged();
virtual void onNewShortcuts(); virtual void onNewShortcuts();
virtual void setCurrentRow0(); virtual void setCurrentRow(int row);
virtual void setCurrentRow1();
virtual void setCurrentRow2();
virtual void setCurrentRow3();
virtual void setCurrentRow4();
virtual void setCurrentRow5();
virtual void setCurrentRow6();
virtual void setCurrentRow7();
virtual void setCurrentRow8();
virtual void setCurrentRow9();
virtual void toggleHeader(); virtual void toggleHeader();
virtual void toggleVHeader();
virtual void copyCurrentRowText();
signals: signals:
void docPreviewClicked(int, Rcl::Doc, int); void docPreviewClicked(int, Rcl::Doc, int);
@ -213,7 +207,25 @@ private:
QShortcut *m_previewsc{nullptr}; QShortcut *m_previewsc{nullptr};
QShortcut *m_showsnipssc{nullptr}; QShortcut *m_showsnipssc{nullptr};
QShortcut *m_showheadersc{nullptr}; QShortcut *m_showheadersc{nullptr};
QShortcut *m_showvheadersc{nullptr};
QShortcut *m_copycurtextsc{nullptr};
}; };
// This is an intermediary object to help setting up multiple similar
// shortcuts with different data (e.g. Ctrl+1, Ctrl+2 etc.). Maybe
// there is another way, but this one works.
class SCData : public QObject {
Q_OBJECT;
public:
SCData(QObject* parent, std::function<void (int)> cb, int row)
: QObject(parent), m_cb(cb), m_row(row) {}
public slots:
virtual void activate() {
m_cb(m_row);
}
private:
std::function<void (int)> m_cb;
int m_row;
};
#endif /* _RESTABLE_H_INCLUDED_ */ #endif /* _RESTABLE_H_INCLUDED_ */