Nicer solution to the activate on click problem

This commit is contained in:
Kovid Goyal 2013-12-12 20:24:47 +05:30
parent ec0d2d7c17
commit 11d7dac241
4 changed files with 23 additions and 12 deletions

View File

@ -6,6 +6,7 @@
#include <QStyle> #include <QStyle>
#include <QApplication> #include <QApplication>
#include <QDebug> #include <QDebug>
#include <QProxyStyle>
QProgressIndicator::QProgressIndicator(QWidget* parent, int size) QProgressIndicator::QProgressIndicator(QWidget* parent, int size)
: QWidget(parent), : QWidget(parent),
@ -159,3 +160,15 @@ bool do_notify(QObject *receiver, QEvent *event) {
return false; return false;
} }
class NoActivateStyle: public QProxyStyle {
public:
int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const {
if (hint == QStyle::SH_ItemView_ActivateItemOnSingleClick) return 0;
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
void set_no_activate_on_click(QWidget *widget) {
widget->setStyle(new NoActivateStyle);
}

View File

@ -102,3 +102,4 @@ int load_style(QString &path, QString &name);
bool do_notify(QObject *receiver, QEvent *event); bool do_notify(QObject *receiver, QEvent *event);
void set_no_activate_on_click(QWidget *widget);

View File

@ -9,6 +9,7 @@
%ModuleHeaderCode %ModuleHeaderCode
int load_style(QString &path, QString &name); int load_style(QString &path, QString &name);
bool do_notify(QObject *receiver, QEvent *event); bool do_notify(QObject *receiver, QEvent *event);
void set_no_activate_on_click(QWidget *widget);
%End %End
class QProgressIndicator : QWidget { class QProgressIndicator : QWidget {
@ -60,3 +61,4 @@ int load_style(QString &path, QString &name);
bool do_notify(QObject *receiver, QEvent *event); bool do_notify(QObject *receiver, QEvent *event);
void set_no_activate_on_click(QWidget *widget);

View File

@ -9,8 +9,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from PyQt4.Qt import ( from PyQt4.Qt import (
QDialog, pyqtSignal, QIcon, QVBoxLayout, QDialogButtonBox, QStackedWidget, QDialog, pyqtSignal, QIcon, QVBoxLayout, QDialogButtonBox, QStackedWidget,
QAction, QMenu, QTreeWidget, QTreeWidgetItem, QGridLayout, QWidget, Qt, QAction, QMenu, QTreeWidget, QTreeWidgetItem, QGridLayout, QWidget, Qt,
QSize, QStyledItemDelegate, QTimer) QSize, QStyledItemDelegate)
from calibre.constants import plugins
from calibre.ebooks.oeb.polish.toc import commit_toc, get_toc from calibre.ebooks.oeb.polish.toc import commit_toc, get_toc
from calibre.gui2 import gprefs, error_dialog from calibre.gui2 import gprefs, error_dialog
from calibre.gui2.toc.main import TOCView, ItemEdit from calibre.gui2.toc.main import TOCView, ItemEdit
@ -125,12 +126,14 @@ class TOCViewer(QWidget):
self.view.setContextMenuPolicy(Qt.CustomContextMenu) self.view.setContextMenuPolicy(Qt.CustomContextMenu)
self.view.customContextMenuRequested.connect(self.show_context_menu, type=Qt.QueuedConnection) self.view.customContextMenuRequested.connect(self.show_context_menu, type=Qt.QueuedConnection)
self.view.itemActivated.connect(self.emit_navigate) self.view.itemActivated.connect(self.emit_navigate)
self.view.itemClicked.connect(self.emit_navigate) pi = plugins['progress_indicator'][0]
if hasattr(pi, 'set_no_activate_on_click'):
pi.set_no_activate_on_click(self.view)
self.view.itemDoubleClicked.connect(self.emit_navigate)
l.addWidget(self.view) l.addWidget(self.view)
self.refresh_action = QAction(QIcon(I('view-refresh.png')), _('&Refresh'), self) self.refresh_action = QAction(QIcon(I('view-refresh.png')), _('&Refresh'), self)
self.refresh_action.triggered.connect(self.build) self.refresh_action.triggered.connect(self.build)
self._last_nav_request = None
def show_context_menu(self, pos): def show_context_menu(self, pos):
menu = QMenu(self) menu = QMenu(self)
@ -156,15 +159,7 @@ class TOCViewer(QWidget):
frag = unicode(item.data(0, FRAG_ROLE).toString()) frag = unicode(item.data(0, FRAG_ROLE).toString())
if not frag: if not frag:
frag = TOP frag = TOP
# Debounce as on some platforms clicking causes both itemActivated self.navigate_requested.emit(dest, frag)
# and itemClicked to be emitted
self._last_nav_request = (dest, frag)
QTimer.singleShot(0, self._emit_navigate)
def _emit_navigate(self):
if self._last_nav_request is not None:
self.navigate_requested.emit(*self._last_nav_request)
self._last_nav_request = None
def build(self): def build(self):
c = current_container() c = current_container()