QtPass  1.2.0
Multi-platform GUI for pass, the standard unix password manager.
deselectabletreeview.h
Go to the documentation of this file.
1 #ifndef DESELECTABLETREEVIEW_H
2 #define DESELECTABLETREEVIEW_H
3 #include "QMouseEvent"
4 #include "QTreeView"
5 #include "mainwindow.h"
6 #include <QTime>
7 
13 class DeselectableTreeView : public QTreeView {
14  Q_OBJECT
15 
16 public:
21  DeselectableTreeView(QWidget *parent) : QTreeView(parent) {}
25  virtual ~DeselectableTreeView() {}
26 
27 signals:
31  void emptyClicked();
32 
33 private:
34  bool doubleClickHappened = false;
35  bool clickSelected = false;
36 
41  virtual void mousePressEvent(QMouseEvent *event) {
42  clickSelected = selectionModel()->isSelected(indexAt(event->pos()));
43  QTreeView::mousePressEvent(event);
44  }
45 
50  void mouseReleaseEvent(QMouseEvent *event) {
51  doubleClickHappened = false;
52  // The timer is to distinguish between single and double click
53  QTime dieTime = QTime::currentTime().addMSecs(200);
54  while (QTime::currentTime() < dieTime)
55  QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
56  // could this be done nicer?
57  if (!doubleClickHappened && clickSelected) {
58  QModelIndex item = indexAt(event->pos());
59  bool selected = selectionModel()->isSelected(indexAt(event->pos()));
60  if ((item.row() == -1 && item.column() == -1) || selected) {
61  clearSelection();
62  const QModelIndex index;
63  selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select);
64  emit emptyClicked();
65  } else {
66  QTreeView::mouseReleaseEvent(event);
67  }
68  } else {
69  QTreeView::mouseReleaseEvent(event);
70  }
71  clickSelected = false;
72  }
73 
78  void mouseDoubleClickEvent(QMouseEvent *event) {
79  doubleClickHappened = true;
80  QTreeView::mouseDoubleClickEvent(event);
81  }
82 };
83 
84 #endif // DESELECTABLETREEVIEW_H
The DeselectableTreeView class loosly based on http://stackoverflow.com/questions/2761284/ thanks to ...
virtual ~DeselectableTreeView()
~DeselectableTreeView standard destructor
void emptyClicked()
emptyClicked event
DeselectableTreeView(QWidget *parent)
DeselectableTreeView standard constructor.