refactor: move MRU ctrl handling code from mainwindow.cc to mruqmenu.cc

This commit is contained in:
shenleban tongying 2023-04-12 04:11:36 -04:00 committed by xiaoyifang
parent 54a4a052a5
commit ddb9655938
5 changed files with 25 additions and 37 deletions

View file

@ -411,7 +411,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
// Tab management // Tab management
tabListMenu = new MRUQMenu(tr("Opened tabs"), ui.tabWidget); tabListMenu = new MRUQMenu(tr("Opened tabs"), ui.tabWidget);
connect( tabListMenu, &MRUQMenu::ctrlReleased, this, &MainWindow::ctrlReleased ); connect( tabListMenu, &MRUQMenu::requestTabChange, ui.tabWidget, &MainTabWidget::setCurrentIndex );
connect( &addTabAction, &QAction::triggered, this, &MainWindow::addNewTab ); connect( &addTabAction, &QAction::triggered, this, &MainWindow::addNewTab );
@ -1815,19 +1815,6 @@ void MainWindow::switchToPrevTab()
ui.tabWidget->setCurrentIndex( ui.tabWidget->currentIndex() - 1 ); ui.tabWidget->setCurrentIndex( ui.tabWidget->currentIndex() - 1 );
} }
//emitted by tabListMenu when user releases Ctrl
void MainWindow::ctrlReleased()
{
if (tabListMenu->actions().size() > 1)
{
QAction *act = tabListMenu->activeAction();
if( act == 0 )
act = tabListMenu->actions().at( 1 );
ui.tabWidget->setCurrentIndex( act->data().toInt() );
}
tabListMenu->hide();
}
void MainWindow::backClicked() void MainWindow::backClicked()
{ {
GD_DPRINTF( "Back\n" ); GD_DPRINTF( "Back\n" );

View file

@ -320,7 +320,6 @@ private slots:
void closeRestTabs(); void closeRestTabs();
void switchToNextTab(); void switchToNextTab();
void switchToPrevTab(); void switchToPrevTab();
void ctrlReleased();
// Handling of active tab list // Handling of active tab list
void createTabList(); void createTabList();

View file

@ -2,20 +2,19 @@
#include <QKeyEvent> #include <QKeyEvent>
MRUQMenu::MRUQMenu(const QString title, QWidget *parent): MRUQMenu::MRUQMenu(const QString title, QWidget *parent):
QMenu(title,parent) QMenu(title,parent)
{ {}
installEventFilter(this);
}
bool MRUQMenu::eventFilter(QObject *obj, QEvent *event) void MRUQMenu::keyReleaseEvent (QKeyEvent * kev){
{ if (kev->key() == Qt::Key_Control && actions().size() > 1){
(void) obj; QAction *act = activeAction();
if (event->type() == QEvent::KeyRelease){ if( act == nullptr ){
QKeyEvent *keyevent = static_cast<QKeyEvent*>(event); act = actions().at( 1 );
if (keyevent->key() == Qt::Key_Control){
emit ctrlReleased();
return true;
}
} }
return false; emit requestTabChange( act->data().toInt() );
} hide();
}
else {
kev->ignore();
}
};

View file

@ -4,20 +4,20 @@
#include <QMenu> #include <QMenu>
#include <QEvent> #include <QEvent>
//The only difference between this class and QMenu is that this class emits // Detail: http://goldendict.org/forum/viewtopic.php?f=4&t=1176
//a signal when Ctrl button is released // When ctrl during ctrl+tab released, a request to change current tab will be emitted.
class MRUQMenu: public QMenu class MRUQMenu: public QMenu
{ {
Q_OBJECT Q_OBJECT
public: public:
MRUQMenu(const QString title, QWidget *parent = 0); explicit MRUQMenu(const QString title, QWidget *parent = 0);
private: private:
bool eventFilter (QObject*, QEvent*); void keyReleaseEvent (QKeyEvent * kev) override;
signals: signals:
void ctrlReleased(); void requestTabChange(int index);
}; };

View file

@ -359,6 +359,9 @@ be the last ones.</string>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QCheckBox" name="mruTabOrder"> <widget class="QCheckBox" name="mruTabOrder">
<property name="toolTip">
<string>MRU order: Most recently used order.</string>
</property>
<property name="text"> <property name="text">
<string>Ctrl-Tab navigates tabs in MRU order</string> <string>Ctrl-Tab navigates tabs in MRU order</string>
</property> </property>