1 #ifndef DATAVIEW_H
2 #define DATAVIEW_H
3 
4 #include "common/extactioncontainer.h"
5 #include "guiSQLiteStudio_global.h"
6 #include <QTabWidget>
7 #include <QMutex>
8 
9 class QToolBar;
10 class SqlQueryView;
11 class SqlQueryModel;
12 class SqlQueryItem;
13 class FormView;
14 class ExtLineEdit;
15 class QLabel;
16 class IntValidator;
17 class WidgetCover;
18 class QScrollArea;
19 class QLineEdit;
20 
21 CFG_KEY_LIST(DataView, QObject::tr("Data view (both grid and form)"),
22      CFG_KEY_ENTRY(REFRESH_DATA,    Qt::Key_F5,                   QObject::tr("Refresh data"))
23      CFG_KEY_ENTRY(SHOW_GRID_VIEW,  Qt::CTRL + Qt::Key_Comma,     QObject::tr("Switch to grid view of the data"))
24      CFG_KEY_ENTRY(SHOW_FORM_VIEW,  Qt::CTRL + Qt::Key_Period,    QObject::tr("Switch to form view of the data"))
25 )
26 
27 class GUI_API_EXPORT DataView : public QTabWidget, public ExtActionContainer
28 {
29         Q_OBJECT
30         Q_ENUMS(Action)
31 
32     public:
33         enum Action
34         {
35             SHOW_GRID_VIEW,
36             SHOW_FORM_VIEW,
37             TABS_ON_TOP,
38             TABS_AT_BOTTOM,
39             // Grid view
40             REFRESH_DATA,
41             FIRST_PAGE,
42             PREV_PAGE,
43             NEXT_PAGE,
44             LAST_PAGE,
45             PAGE_EDIT,
46             FILTER_VALUE,
47             FILTER,
48             FILTER_STRING,
49             FILTER_SQL,
50             FILTER_REGEXP,
51             FILTER_PER_COLUMN,
52             GRID_TOTAL_ROWS,
53             SELECTIVE_COMMIT,
54             SELECTIVE_ROLLBACK,
55             INSERT_ROW_BEFORE,
56             INSERT_ROW_AFTER,
57             INSERT_ROW_AT_END,
58             // Form view
59             FORM_TOTAL_ROWS,
60             FORM_CURRENT_ROW
61         };
62 
63         enum class ActionGroup
64         {
65             FILTER_MODE,
66             TABS_POSITION,
67             INSERT_ROW_POSITIONING
68         };
69 
70         enum ToolBar
71         {
72             TOOLBAR_GRID,
73             TOOLBAR_FORM
74         };
75 
76         explicit DataView(QWidget *parent = 0);
77 
78         void init(SqlQueryModel* model);
79 
80         FormView* getFormView() const;
81         SqlQueryView* getGridView() const;
82         SqlQueryModel* getModel() const;
83         QToolBar* getToolBar(int toolbar) const;
84         bool isUncommitted() const;
85 
86         static void staticInit();
87         static void insertAction(ExtActionPrototype* action, ToolBar toolbar = TOOLBAR_GRID);
88         static void insertActionBefore(ExtActionPrototype* action, Action beforeAction, ToolBar toolbar = TOOLBAR_GRID);
89         static void insertActionAfter(ExtActionPrototype* action, Action afterAction, ToolBar toolbar = TOOLBAR_GRID);
90         static void removeAction(ExtActionPrototype* action, ToolBar toolbar = TOOLBAR_GRID);
91 
92     protected:
93         void createActions();
94         void setupDefShortcuts();
95         void resizeColumnsInitiallyToContents();
96 
97     private:
98         enum class TabsPosition
99         {
100             TOP,
101             BOTTOM
102         };
103 
104         enum class IndexModifier
105         {
106             FIRST,
107             PREV,
108             NEXT,
109             LAST
110         };
111 
112         enum class FilterMode
113         {
114             STRING,
115             SQL,
116             REGEXP
117         };
118 
119         static void createStaticActions();
120         static void loadTabsMode();
121 
122         void initFormView();
123         void initFilter();
124         void initUpdates();
125         void initSlots();
126         void initPageEdit();
127         void initWidgetCover();
128         void createContents();
129         void createFilterPanel();
130         void goToFormRow(IndexModifier idxMod);
131         void setNavigationState(bool enabled);
132         void updateNavigationState();
133         void updateGridNavigationState();
134         void goToPage(const QString& pageStr);
135         void updatePageEdit();
136         void updateResultsCount(int resultsCount);
137         void updateCurrentFormViewRow();
138         void setFormViewEnabled(bool enabled);
139         void readData();
140         void initFormViewForNewRow();
141         void formViewFocusFirstEditor();
142         void recreateFilterInputs();
143         void createFilteringActions();
144 
145         static TabsPosition tabsPosition;
146         static QHash<Action,QAction*> staticActions;
147         static QHash<ActionGroup,QActionGroup*> staticActionGroups;
148 
149         FilterMode filterMode = FilterMode::STRING;
150         QToolBar* gridToolBar = nullptr;
151         QToolBar* formToolBar = nullptr;
152         SqlQueryView* gridView = nullptr;
153         SqlQueryModel* model = nullptr;
154         FormView* formView = nullptr;
155         QWidget* gridWidget = nullptr;
156         QWidget* formWidget = nullptr;
157         QScrollArea* perColumnFilterArea = nullptr;
158         QWidget* perColumnWidget = nullptr;
159         QWidget* perColumnAreaParent = nullptr;
160         ExtLineEdit* filterEdit = nullptr;
161         QLabel* rowCountLabel = nullptr;
162         QLabel* formViewRowCountLabel = nullptr;
163         QLabel* formViewCurrentRowLabel = nullptr;
164         ExtLineEdit* pageEdit = nullptr;
165         IntValidator* pageValidator = nullptr;
166         bool navigationState = false;
167         bool totalPagesAvailable = false;
168         QMutex manualPageChangeMutex;
169         bool uncommittedGrid = false;
170         bool uncommittedForm = false;
171         WidgetCover* widgetCover = nullptr;
172         QList<QLineEdit*> filterInputs;
173         QStringList filterValues;
174         QWidget* filterLeftSpacer = nullptr;
175         QWidget* filterRightSpacer = nullptr;
176 
177     signals:
178 
179     public slots:
180         void refreshData();
181 
182     private slots:
183         void dataLoadingEnded(bool successful);
184         void executionSuccessful();
185         void totalRowsAndPagesAvailable();
186         void insertRow();
187         void insertMultipleRows();
188         void deleteRow();
189         void commitGrid();
190         void rollbackGrid();
191         void selectiveCommitGrid();
192         void selectiveRollbackGrid();
193         void firstPage();
194         void prevPage();
195         void nextPage();
196         void lastPage();
197         void pageEntered();
198         void applyFilter();
199         void resetFilter();
200         void commitForm();
201         void rollbackForm();
202         void firstRow();
203         void prevRow();
204         void nextRow();
205         void lastRow();
206         void columnsHeaderClicked(int columnIdx);
207         void tabChanged(int newIndex);
208         void updateFormNavigationState();
209         void updateFormCommitRollbackActions();
210         void updateCommitRollbackActions(bool enabled);
211         void updateSelectiveCommitRollbackActions(bool enabled);
212         void showGridView();
213         void showFormView();
214         void updateTabsMode();
215         void filterModeSelected();
216         void coverForGridCommit(int total);
217         void updateGridCommitCover(int value);
218         void hideGridCommitCover();
219         void adjustColumnWidth(SqlQueryItem* item);
220         void syncFilterScrollPosition();
221         void resizeFilter(int section, int oldSize, int newSize);
222         void togglePerColumnFiltering();
223 };
224 
225 int qHash(DataView::ActionGroup action);
226 
227 #endif // DATAVIEW_H
228