QtPass Developer documentation
QtPass test coverage
Current view: top level - src/deselectabletreeview.h (source / functions) Coverage Total Hit
Test: QtPass Lines: 84.4 % 32 27
Test Date: 2026-12-03 23:34:20 Functions: 83.3 % 6 5
Branches: 66.7 % 12 8

             Branch data     Line data    Source code
       1                 :             : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
       2                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       3                 :             : /**
       4                 :             :  * @file
       5                 :             :  * @brief DeselectableTreeView: a QTreeView that clears its selection when
       6                 :             :  *        empty space or an already-selected row is clicked.
       7                 :             :  */
       8                 :             : #ifndef SRC_DESELECTABLETREEVIEW_H_
       9                 :             : #define SRC_DESELECTABLETREEVIEW_H_
      10                 :             : 
      11                 :             : #include <QGuiApplication>
      12                 :             : #include <QMouseEvent>
      13                 :             : #include <QPersistentModelIndex>
      14                 :             : #include <QStyleHints>
      15                 :             : #include <QTimer>
      16                 :             : #include <QTreeView>
      17                 :             : 
      18                 :             : /**
      19                 :             :  * @class DeselectableTreeView
      20                 :             :  * @ingroup ui
      21                 :             :  * @brief A QTreeView that clears its selection, and emits emptyClicked(),
      22                 :             :  *        when the user clicks empty space or an already-selected item.
      23                 :             :  *
      24                 :             :  * The press records whether the row under the pointer was selected, the
      25                 :             :  * release handles selection immediately and arms a single-shot timer for the
      26                 :             :  * platform's double-click interval, and only when no double-click arrives in
      27                 :             :  * that time does deselect() clear the selection. Opening an entry by
      28                 :             :  * double-click therefore always wins over deselecting it.
      29                 :             :  */
      30                 :             : class DeselectableTreeView : public QTreeView {
      31                 :             :   Q_OBJECT
      32                 :             : 
      33                 :             : public:
      34                 :             :   /**
      35                 :             :    * @brief Construct the view and arm the single-shot deselect timer.
      36                 :             :    * @param parent Parent widget forwarded to QTreeView.
      37                 :             :    */
      38                 :         206 :   explicit DeselectableTreeView(QWidget *parent) : QTreeView(parent) {
      39                 :         206 :     deselectTimer.setSingleShot(true);
      40                 :         206 :     connect(&deselectTimer, &QTimer::timeout, this,
      41                 :         206 :             &DeselectableTreeView::deselect);
      42                 :         206 :   }
      43                 :             :   /**
      44                 :             :    * @brief Default destructor.
      45                 :             :    */
      46                 :         412 :   ~DeselectableTreeView() override = default;
      47                 :             : 
      48                 :             : signals:
      49                 :             :   /**
      50                 :             :    * @brief Emitted after deselect() has cleared the selection because of a
      51                 :             :    *        single click on empty space or on a selected row; MainWindow
      52                 :             :    *        clears the password panel in response.
      53                 :             :    */
      54                 :             :   void emptyClicked();
      55                 :             : 
      56                 :             : private:
      57                 :             :   /**
      58                 :             :    * @brief Set by mouseDoubleClickEvent() so a deselect() that fires after a
      59                 :             :    *        double-click's first release does nothing.
      60                 :             :    */
      61                 :             :   bool doubleClickHappened = false;
      62                 :             :   /**
      63                 :             :    * @brief Whether the row under the last press was already selected; read
      64                 :             :    *        and reset by the matching release.
      65                 :             :    */
      66                 :             :   bool clickSelected = false;
      67                 :             :   /**
      68                 :             :    * @brief Single-shot timer, started by a qualifying release for one
      69                 :             :    *        double-click interval; its timeout runs deselect().
      70                 :             :    */
      71                 :             :   QTimer deselectTimer;
      72                 :             : 
      73                 :             :   /**
      74                 :             :    * @brief Remember whether the pressed row was already selected and cancel
      75                 :             :    *        any pending deselect from the previous click.
      76                 :             :    * @param event The mouse press; forwarded to QTreeView.
      77                 :             :    */
      78                 :           7 :   void mousePressEvent(QMouseEvent *event) override {
      79                 :             :     // A new press belongs to a new click: whatever the previous release left
      80                 :             :     // pending must not clear the selection this press is about to make.
      81                 :           7 :     deselectTimer.stop();
      82                 :           7 :     clickSelected =
      83                 :           7 :         selectionModel()->isSelected(indexAt(event->position().toPoint()));
      84                 :           7 :     QTreeView::mousePressEvent(event);
      85                 :           7 :   }
      86                 :             : 
      87                 :             :   /**
      88                 :             :    * @brief Let QTreeView handle the release at once, then, if the press was
      89                 :             :    *        on a selected row and the release is over empty space or a still
      90                 :             :    *        selected row, start deselectTimer.
      91                 :             :    * @param event The mouse release; forwarded to QTreeView first.
      92                 :             :    */
      93                 :           7 :   void mouseReleaseEvent(QMouseEvent *event) override {
      94                 :             :     // Persistent: QTreeView::mouseReleaseEvent() emits clicked(), and what
      95                 :             :     // that runs can change the model (a message box for a link, the file
      96                 :             :     // system model reloading meanwhile); a plain index then names another
      97                 :             :     // row, or memory that is gone.
      98                 :           7 :     const QPersistentModelIndex item(indexAt(event->position().toPoint()));
      99                 :           7 :     const bool pressWasOnSelection = clickSelected;
     100                 :           7 :     clickSelected = false;
     101                 :             : 
     102                 :             :     // Handle the release immediately: waiting here would delay selection —
     103                 :             :     // and therefore every decrypt — by the double-click interval.
     104                 :           7 :     QTreeView::mouseReleaseEvent(event);
     105                 :             : 
     106                 :             :     const bool emptyOrSelected =
     107   [ +  +  -  +  :           7 :         !item.isValid() || selectionModel()->isSelected(item);
             +  +  -  + ]
     108         [ +  + ]:           7 :     if (!pressWasOnSelection || !emptyOrSelected)
     109                 :             :       return;
     110                 :             : 
     111                 :             :     // Deselecting is the one decision that must wait: a double-click starts
     112                 :             :     // with a release, and opening the editor must win over clearing the
     113                 :             :     // selection. Ask the platform how long a double-click may take instead
     114                 :             :     // of assuming 200 ms.
     115                 :           5 :     doubleClickHappened = false;
     116                 :           5 :     deselectTimer.start(
     117                 :             :         QGuiApplication::styleHints()->mouseDoubleClickInterval());
     118                 :           7 :   }
     119                 :             : 
     120                 :             :   /**
     121                 :             :    * @brief Timer slot: clear the selection and current index and emit
     122                 :             :    *        emptyClicked(), unless a double-click happened in the meantime.
     123                 :             :    */
     124                 :           0 :   void deselect() {
     125         [ #  # ]:           0 :     if (doubleClickHappened)
     126                 :             :       return;
     127                 :           0 :     clearSelection();
     128                 :           0 :     selectionModel()->setCurrentIndex(QModelIndex(),
     129                 :             :                                       QItemSelectionModel::Select);
     130                 :           0 :     emit emptyClicked();
     131                 :             :   }
     132                 :             : 
     133                 :             :   /**
     134                 :             :    * @brief Cancel the pending deselect so the double-click opens the entry
     135                 :             :    *        instead of clearing the selection.
     136                 :             :    * @param event The double-click; forwarded to QTreeView.
     137                 :             :    */
     138                 :           1 :   void mouseDoubleClickEvent(QMouseEvent *event) override {
     139                 :           1 :     doubleClickHappened = true;
     140                 :           1 :     deselectTimer.stop();
     141                 :           1 :     QTreeView::mouseDoubleClickEvent(event);
     142                 :           1 :   }
     143                 :             : };
     144                 :             : 
     145                 :             : #endif // SRC_DESELECTABLETREEVIEW_H_
        

Generated by: LCOV version 2.0-1