Skip to content

Commit

Permalink
Only deselect on mouseReleaseEvent if the mousePressEvent was on a se…
Browse files Browse the repository at this point in the history
…lected item,

fixes #233
  • Loading branch information
annejan committed Nov 22, 2016
1 parent 3cc102f commit ab4a3b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
24 changes: 19 additions & 5 deletions deselectabletreeview.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,43 @@ class DeselectableTreeView : public QTreeView {
void emptyClicked();

private:
bool DoubleClickHappened;
bool doubleClickHappened = false;
bool clickSelected = false;

/**
* @brief mousePressEvent registers if the field was pre-selected
* @param event
*/
virtual void mousePressEvent(QMouseEvent *event) {
clickSelected = selectionModel()->isSelected(indexAt(event->pos()));
QTreeView::mousePressEvent(event);
}

/**
* @brief mouseReleaseEvent now deselects on click on empty space
* @param event
*/
void mouseReleaseEvent(QMouseEvent *event) {
DoubleClickHappened = false;
doubleClickHappened = false;
// The timer is to distinguish between single and double click
QTime dieTime= QTime::currentTime().addMSecs(200);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
if (!DoubleClickHappened){
if (!doubleClickHappened && clickSelected){
QModelIndex item = indexAt(event->pos());
bool selected = selectionModel()->isSelected(indexAt(event->pos()));
QTreeView::mouseReleaseEvent(event);
if ((item.row() == -1 && item.column() == -1) || selected) {
clearSelection();
const QModelIndex index;
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select);
emit emptyClicked();
} else {
QTreeView::mouseReleaseEvent(event);
}
} else {
QTreeView::mouseReleaseEvent(event);
}
clickSelected = false;
}


Expand All @@ -63,7 +77,7 @@ class DeselectableTreeView : public QTreeView {
* @param event
*/
void mouseDoubleClickEvent(QMouseEvent *event){
DoubleClickHappened = true;
doubleClickHappened = true;
QTreeView::mouseDoubleClickEvent(event);
}

Expand Down
1 change: 0 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,6 @@ void MainWindow::copyTextToClipboard(const QString &text) {
clippedText = text;
ui->statusBar->showMessage(tr("Copied to clipboard"), 2000);
if (QtPassSettings::isUseAutoclear()) {
qDebug() << QtPassSettings::getAutoclearSeconds();
QTimer::singleShot(1000 * QtPassSettings::getAutoclearSeconds(), this,
SLOT(clearClipboard()));
}
Expand Down

0 comments on commit ab4a3b5

Please sign in to comment.