QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 DatabaseQueryHistoryNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
38 , mEntry( entry )
39 , mProvider( provider )
40 {
41 }
42
43 protected:
44 QgsHistoryEntry mEntry;
45 QgsDatabaseQueryHistoryProvider *mProvider = nullptr;
46};
47
48class DatabaseQueryValueNode : public DatabaseQueryHistoryNode
49{
50 public:
51 DatabaseQueryValueNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider, const QString &value )
52 : DatabaseQueryHistoryNode( entry, provider )
53 , mValue( value )
54 {}
55
56 QVariant data( int role = Qt::DisplayRole ) const override
57 {
58 switch ( role )
59 {
60 case Qt::DisplayRole:
61 case Qt::ToolTipRole:
62 return mValue;
63
64 default:
65 return QVariant();
66 }
67 }
68
69 QString html( const QgsHistoryWidgetContext & ) const override
70 {
71 return mValue;
72 }
73
74 private:
75 QString mValue;
76};
77
78class DatabaseQueryRootNode : public DatabaseQueryHistoryNode
79{
80 public:
81 DatabaseQueryRootNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
82 : DatabaseQueryHistoryNode( entry, provider )
83 {
84 setEntry( entry );
85
86 mProviderKey = mEntry.entry.value( QStringLiteral( "provider" ) ).toString();
87 }
88
89 QVariant data( int role = Qt::DisplayRole ) const override
90 {
91 switch ( role )
92 {
93 case Qt::DisplayRole:
94 case Qt::ToolTipRole:
95 return mEntry.entry.value( QStringLiteral( "query" ) );
96
97 case Qt::DecorationRole:
98 {
99 if ( !mProviderIcon.isNull() )
100 return mProviderIcon;
101
102 if ( QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( mProviderKey ) )
103 {
104 mProviderIcon = md->icon();
105 }
106 return mProviderIcon;
107 }
108
109 default:
110 break;
111 }
112 return QVariant();
113 }
114
115 void setEntry( const QgsHistoryEntry &entry )
116 {
117 if ( !mConnectionNode )
118 {
119 mConnectionNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Connection: %1" ).arg( entry.entry.value( QStringLiteral( "connection" ) ).toString() ) );
120 addChild( mConnectionNode );
121 }
122 if ( entry.entry.contains( QStringLiteral( "rows" ) ) )
123 {
124 if ( !mRowsNode )
125 {
126 mRowsNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Row count: %1" ).arg( entry.entry.value( QStringLiteral( "rows" ) ).toString() ) );
127 addChild( mRowsNode );
128 }
129 }
130 if ( entry.entry.contains( QStringLiteral( "time" ) ) )
131 {
132 if ( !mTimeNode )
133 {
134 mTimeNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Execution time: %1 ms" ).arg( entry.entry.value( QStringLiteral( "time" ) ).toString() ) );
135 addChild( mTimeNode );
136 }
137 }
138 }
139
140 QWidget *createWidget( const QgsHistoryWidgetContext & ) override
141 {
142 QgsCodeEditorSQL *editor = new QgsCodeEditorSQL();
143 editor->setText( mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
144 editor->setReadOnly( true );
145 editor->setCaretLineVisible( false );
146 editor->setLineNumbersVisible( false );
147 editor->setFoldingVisible( false );
148 editor->setEdgeMode( QsciScintilla::EdgeNone );
149 editor->setWrapMode( QsciScintilla::WrapMode::WrapWord );
150 return editor;
151 }
152
153 bool doubleClicked( const QgsHistoryWidgetContext & ) override
154 {
155 mProvider->emitOpenSqlDialog( mEntry.entry.value( QStringLiteral( "connection" ) ).toString(), mEntry.entry.value( QStringLiteral( "provider" ) ).toString(), mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
156 return true;
157 }
158
159 void populateContextMenu( QMenu *menu, const QgsHistoryWidgetContext & ) override
160 {
161 QAction *executeAction = new QAction(
162 QObject::tr( "Execute SQL Command…" ), menu
163 );
164 QObject::connect( executeAction, &QAction::triggered, menu, [=] {
165 mProvider->emitOpenSqlDialog( mEntry.entry.value( QStringLiteral( "connection" ) ).toString(), mEntry.entry.value( QStringLiteral( "provider" ) ).toString(), mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
166 } );
167 menu->addAction( executeAction );
168
169 QAction *copyAction = new QAction(
170 QObject::tr( "Copy SQL Command" ), menu
171 );
172 copyAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionEditCopy.svg" ) ) );
173 QObject::connect( copyAction, &QAction::triggered, menu, [=] {
174 QMimeData *m = new QMimeData();
175 m->setText( mEntry.entry.value( QStringLiteral( "query" ) ).toString() );
176 QApplication::clipboard()->setMimeData( m );
177 } );
178 menu->addAction( copyAction );
179 }
180
181 private:
182 QString mProviderKey;
183 mutable QIcon mProviderIcon;
184 DatabaseQueryValueNode *mConnectionNode = nullptr;
185 DatabaseQueryValueNode *mRowsNode = nullptr;
186 DatabaseQueryValueNode *mTimeNode = nullptr;
187};
188
190
191
195
197{
198 return QStringLiteral( "dbquery" );
199}
200
202{
203 return new DatabaseQueryRootNode( entry, this );
204}
205
207{
208 if ( DatabaseQueryRootNode *dbNode = dynamic_cast<DatabaseQueryRootNode *>( node ) )
209 {
210 dbNode->setEntry( entry );
211 }
212}
213
214void QgsDatabaseQueryHistoryProvider::emitOpenSqlDialog( const QString &connectionUri, const QString &provider, const QString &sql )
215{
216 emit openSqlDialog( connectionUri, provider, sql );
217}
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.