1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (c) 2019, The Regents of the University of California
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // * Redistributions of source code must retain the above copyright notice, this
11 //   list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above copyright notice,
14 //   this list of conditions and the following disclaimer in the documentation
15 //   and/or other materials provided with the distribution.
16 //
17 // * Neither the name of the copyright holder nor the names of its
18 //   contributors may be used to endorse or promote products derived from
19 //   this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 #pragma once
34 
35 #include <QAbstractItemDelegate>
36 #include <QAbstractTableModel>
37 #include <QAction>
38 #include <QDockWidget>
39 #include <QItemDelegate>
40 #include <QMainWindow>
41 #include <QMenu>
42 #include <QModelIndex>
43 #include <QPoint>
44 #include <QShortcut>
45 #include <QStringList>
46 #include <QStyledItemDelegate>
47 #include <QToolBar>
48 #include <QVariant>
49 #include <unordered_map>
50 
51 #include "gui/gui.h"
52 #include "ui_selectedWidget.h"
53 
54 namespace gui {
55 class SelectionModel : public QAbstractTableModel
56 {
57   Q_OBJECT
58  public:
59   SelectionModel(const SelectionSet& objs);
60 
61   int rowCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
62   int columnCount(const QModelIndex& parent
63                   = QModelIndex()) const Q_DECL_OVERRIDE;
64 
65   QVariant data(const QModelIndex& index,
66                 int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
67   QVariant headerData(int section,
68                       Qt::Orientation orientation,
69                       int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
70 
getItemAt(int idx)71   const Selected* getItemAt(int idx) const { return table_data_[idx]; }
72 
73   void populateModel();
74 
setDb(odb::dbDatabase * db)75   void setDb(odb::dbDatabase* db) { db_ = db; }
76 
77  private:
78   odb::dbDatabase* db_;
79   const SelectionSet& objs_;
80   std::vector<const Selected*> table_data_;
81 };
82 
83 class HighlightModel : public QAbstractTableModel
84 {
85   Q_OBJECT
86  public:
87   HighlightModel(const HighlightSet& objs);
88 
89   int rowCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
90   int columnCount(const QModelIndex& parent
91                   = QModelIndex()) const Q_DECL_OVERRIDE;
92 
93   QVariant data(const QModelIndex& index,
94                 int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
95   QVariant headerData(int section,
96                       Qt::Orientation orientation,
97                       int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
98 
getItemAt(int idx)99   const Selected* getItemAt(int idx) const { return table_data_[idx].second; }
100   void populateModel();
101 
102   int highlightGroup(const QModelIndex& index) const;
103   bool setData(const QModelIndex& index,
104                const QVariant& value,
105                int role) override;
106 
setDb(odb::dbDatabase * db)107   void setDb(odb::dbDatabase* db) { db_ = db; }
108 
109  private:
110   odb::dbDatabase* db_;
111   const HighlightSet& objs_;
112   std::vector<std::pair<int, const Selected*>> table_data_;
113 };
114 
115 class HighlightGroupDelegate : public QStyledItemDelegate
116 {
117   Q_OBJECT
118  public:
119   HighlightGroupDelegate(QObject* parent = 0);
120 
121   QWidget* createEditor(QWidget* parent,
122                         const QStyleOptionViewItem& option,
123                         const QModelIndex& index) const;
124   void setEditorData(QWidget* editor, const QModelIndex& index) const;
125   void setModelData(QWidget* editor,
126                     QAbstractItemModel* model,
127                     const QModelIndex& index) const;
128   void updateEditorGeometry(QWidget* editor,
129                             const QStyleOptionViewItem& option,
130                             const QModelIndex& index) const;
131   void paint(QPainter* painter,
132              const QStyleOptionViewItem& option,
133              const QModelIndex& index) const;
134 
135  private:
136   std::vector<std::string> items_;
137   HighlightModel* table_model_;
138 };
139 
140 class SelectHighlightWindow : public QDockWidget
141 {
142   Q_OBJECT
143 
144  public:
145   explicit SelectHighlightWindow(const SelectionSet& selSet,
146                                  const HighlightSet& hltSet,
147                                  QWidget* parent = nullptr);
148   ~SelectHighlightWindow();
149 
150   void setDb(odb::dbDatabase* db);
151 
152  signals:
153   void clearAllSelections();
154   void clearAllHighlights();
155 
156   void clearSelectedItems(const QList<const Selected*>& items);
157   void clearHighlightedItems(const QList<const Selected*>& items);
158   void zoomInToItems(const QList<const Selected*>& items);
159   void highlightSelectedItemsSig(const QList<const Selected*>& items,
160                                  int highlight_group);
161 
162  public slots:
163   void updateSelectionModel();
164   void updateHighlightModel();
165   void showSelectCustomMenu(QPoint pos);
166   void showHighlightCustomMenu(QPoint pos);
167 
168   void deselectItems();
169   void highlightSelectedItems();
170   void zoomInSelectedItems();
171   void dehighlightItems();
172   void zoomInHighlightedItems();
173 
174  private:
175   Ui::SelectHighlightWidget* ui;
176   SelectionModel selection_model_;
177   HighlightModel highlight_model_;
178 
179   QMenu* select_context_menu_;
180   QMenu* highlight_context_menu_;
181 };
182 
183 }  // namespace gui
184