QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsdbqueryhistoryprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdbqueryhistoryprovider.cpp
3 --------------------------
4 begin : April 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18#include "moc_qgsdbqueryhistoryprovider.cpp"
19#include "qgscodeeditorsql.h"
20#include "qgshistoryentry.h"
21#include "qgsprovidermetadata.h"
22#include "qgsproviderregistry.h"
23#include "qgsapplication.h"
24
25#include <QIcon>
26#include <QAction>
27#include <QMenu>
28#include <QMimeData>
29#include <QClipboard>
30
32
33class DatabaseQueryHistoryNode : public QgsHistoryEntryGroup
34{
35 public:
36
37 DatabaseQueryHistoryNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
39 , mEntry( entry )
40 , mProvider( provider )
41 {
42 }
43
44 protected:
45
46 QgsHistoryEntry mEntry;
47 QgsDatabaseQueryHistoryProvider *mProvider = nullptr;
48
49};
50
51class DatabaseQueryValueNode : public DatabaseQueryHistoryNode
52{
53 public:
54
55 DatabaseQueryValueNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider, const QString &value )
56 : DatabaseQueryHistoryNode( entry, provider )
57 , mValue( value )
58 {}
59
60 QVariant data( int role = Qt::DisplayRole ) const override
61 {
62 switch ( role )
63 {
64 case Qt::DisplayRole:
65 case Qt::ToolTipRole:
66 return mValue;
67
68 default:
69 return QVariant();
70 }
71 }
72
73 QString html( const QgsHistoryWidgetContext & ) const override
74 {
75 return mValue;
76 }
77
78 private:
79
80 QString mValue;
81
82};
83
84class DatabaseQueryRootNode : public DatabaseQueryHistoryNode
85{
86 public:
87
88 DatabaseQueryRootNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
89 : DatabaseQueryHistoryNode( entry, provider )
90 {
91 setEntry( entry );
92
93 mProviderKey = mEntry.entry.value( QStringLiteral( "provider" ) ).toString();
94 }
95
96 QVariant data( int role = Qt::DisplayRole ) const override
97 {
98 switch ( role )
99 {
100 case Qt::DisplayRole:
101 case Qt::ToolTipRole:
102 return mEntry.entry.value( QStringLiteral( "query" ) );
103
104 case Qt::DecorationRole:
105 {
106 if ( !mProviderIcon.isNull() )
107 return mProviderIcon;
108
109 if ( QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( mProviderKey ) )
110 {
111 mProviderIcon = md->icon();
112 }
113 return mProviderIcon;
114 }
115
116 default:
117 break;
118 }
119 return QVariant();
120 }
121
122 void setEntry( const QgsHistoryEntry &entry )
123 {
124 if ( !mConnectionNode )
125 {
126 mConnectionNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Connection: %1" ).arg( entry.entry.value( QStringLiteral( "connection" ) ).toString() ) );
127 addChild( mConnectionNode );
128 }
129 if ( entry.entry.contains( QStringLiteral( "rows" ) ) )
130 {
131 if ( !mRowsNode )
132 {
133 mRowsNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Row count: %1" ).arg( entry.entry.value( QStringLiteral( "rows" ) ).toString() ) );
134 addChild( mRowsNode );
135 }
136 }
137 if ( entry.entry.contains( QStringLiteral( "time" ) ) )
138 {
139 if ( !mTimeNode )
140 {
141 mTimeNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Execution time: %1 ms" ).arg( entry.entry.value( QStringLiteral( "time" ) ).toString() ) );
142 addChild( mTimeNode );
143 }
144 }
145 }
146
147 QWidget *createWidget( const QgsHistoryWidgetContext & ) override
148 {
149 QgsCodeEditorSQL *editor = new QgsCodeEditorSQL();
150 editor->setText( mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
151 editor->setReadOnly( true );
152 editor->setCaretLineVisible( false );
153 editor->setLineNumbersVisible( false );
154 editor->setFoldingVisible( false );
155 editor->setEdgeMode( QsciScintilla::EdgeNone );
156 editor->setWrapMode( QsciScintilla::WrapMode::WrapWord );
157 return editor;
158 }
159
160 bool doubleClicked( const QgsHistoryWidgetContext & ) override
161 {
162 mProvider->emitOpenSqlDialog( mEntry.entry.value( QStringLiteral( "connection" ) ).toString(),
163 mEntry.entry.value( QStringLiteral( "provider" ) ).toString(),
164 mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
165 return true;
166 }
167
168 void populateContextMenu( QMenu *menu, const QgsHistoryWidgetContext & ) override
169 {
170 QAction *executeAction = new QAction(
171 QObject::tr( "Execute SQL Command…" ), menu );
172 QObject::connect( executeAction, &QAction::triggered, menu, [ = ]
173 {
174 mProvider->emitOpenSqlDialog( mEntry.entry.value( QStringLiteral( "connection" ) ).toString(),
175 mEntry.entry.value( QStringLiteral( "provider" ) ).toString(),
176 mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
177 } );
178 menu->addAction( executeAction );
179
180 QAction *copyAction = new QAction(
181 QObject::tr( "Copy SQL Command" ), menu );
182 copyAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionEditCopy.svg" ) ) );
183 QObject::connect( copyAction, &QAction::triggered, menu, [ = ]
184 {
185 QMimeData *m = new QMimeData();
186 m->setText( mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
187 QApplication::clipboard()->setMimeData( m );
188 } );
189 menu->addAction( copyAction );
190 }
191
192 private:
193
194 QString mProviderKey;
195 mutable QIcon mProviderIcon;
196 DatabaseQueryValueNode *mConnectionNode = nullptr;
197 DatabaseQueryValueNode *mRowsNode = nullptr;
198 DatabaseQueryValueNode *mTimeNode = nullptr;
199
200};
201
203
204
208
210{
211 return QStringLiteral( "dbquery" );
212}
213
215{
216 return new DatabaseQueryRootNode( entry, this );
217}
218
220{
221 if ( DatabaseQueryRootNode *dbNode = dynamic_cast< DatabaseQueryRootNode * >( node ) )
222 {
223 dbNode->setEntry( entry );
224 }
225}
226
227void QgsDatabaseQueryHistoryProvider::emitOpenSqlDialog( const QString &connectionUri, const QString &provider, const QString &sql )
228{
229 emit openSqlDialog( connectionUri, provider, sql );
230}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A SQL editor based on QScintilla2.
void setFoldingVisible(bool folding)
Set whether the folding controls are visible in the editor.
void setLineNumbersVisible(bool visible)
Sets whether line numbers should be visible in the editor.
History provider for operations database queries.
void emitOpenSqlDialog(const QString &connectionUri, const QString &provider, const QString &sql)
Causes the provider to emit the openSqlDialog() signal.
void updateNodeForEntry(QgsHistoryEntryNode *node, const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context) override
Updates an existing history node for the given entry.
QString id() const override
Returns the provider's unique id, which is used to associate existing history entries with the provid...
void openSqlDialog(const QString &connectionUri, const QString &provider, const QString &sql)
Emitted when the provider wants to trigger a SQL execution dialog.
QgsHistoryEntryNode * createNodeForEntry(const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context) override
Creates a new history node for the given entry.
Base class for history entry "group" nodes, which contain children of their own.
Base class for nodes representing a QgsHistoryEntry.
Encapsulates a history entry.
QVariantMap entry
Entry details.
Contains settings which reflect the context in which a history widget is shown, e....
Holds data provider key, description, and associated shared library file or function pointer informat...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.