QGIS API Documentation 3.99.0-Master (8e76e220402)
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
19#include "qgsapplication.h"
20#include "qgscodeeditorsql.h"
21#include "qgshistoryentry.h"
23#include "qgsprovidermetadata.h"
24#include "qgsproviderregistry.h"
25
26#include <QAction>
27#include <QClipboard>
28#include <QIcon>
29#include <QMenu>
30#include <QMimeData>
31#include <QString>
32
33#include "moc_qgsdbqueryhistoryprovider.cpp"
34
35using namespace Qt::StringLiterals;
36
38
39class DatabaseQueryHistoryNode : public QgsHistoryEntryGroup
40{
41 public:
42 DatabaseQueryHistoryNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
43 : QgsHistoryEntryGroup()
44 , mEntry( entry )
45 , mProvider( provider )
46 {
47 }
48
49 protected:
50 QgsHistoryEntry mEntry;
51 QgsDatabaseQueryHistoryProvider *mProvider = nullptr;
52};
53
54class DatabaseQueryValueNode : public DatabaseQueryHistoryNode
55{
56 public:
57 DatabaseQueryValueNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider, const QString &value )
58 : DatabaseQueryHistoryNode( entry, provider )
59 , mValue( value )
60 {}
61
62 QVariant data( int role = Qt::DisplayRole ) const override
63 {
64 switch ( role )
65 {
66 case Qt::DisplayRole:
67 case Qt::ToolTipRole:
68 return mValue;
69
70 default:
71 return QVariant();
72 }
73 }
74
75 QString html( const QgsHistoryWidgetContext & ) const override
76 {
77 return mValue;
78 }
79
80 private:
81 QString mValue;
82};
83
84class DatabaseQueryRootNode : public DatabaseQueryHistoryNode
85{
86 public:
87 DatabaseQueryRootNode( const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
88 : DatabaseQueryHistoryNode( entry, provider )
89 {
90 setEntry( entry );
91
92 mProviderKey = mEntry.entry.value( u"provider"_s ).toString();
93 }
94
95 QVariant data( int role = Qt::DisplayRole ) const override
96 {
97 switch ( role )
98 {
99 case Qt::DisplayRole:
100 case Qt::ToolTipRole:
101 return mEntry.entry.value( u"query"_s );
102
103 case Qt::DecorationRole:
104 {
105 if ( !mProviderIcon.isNull() )
106 return mProviderIcon;
107
108 if ( QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( mProviderKey ) )
109 {
110 mProviderIcon = md->icon();
111 }
112 return mProviderIcon;
113 }
114
115 default:
116 break;
117 }
118 return QVariant();
119 }
120
121 void setEntry( const QgsHistoryEntry &entry )
122 {
123 if ( !mConnectionNode )
124 {
125 mConnectionNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Connection: %1" ).arg( entry.entry.value( u"connection"_s ).toString() ) );
126 addChild( mConnectionNode );
127 }
128 if ( entry.entry.contains( u"rows"_s ) )
129 {
130 if ( !mRowsNode )
131 {
132 mRowsNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Row count: %1" ).arg( entry.entry.value( u"rows"_s ).toString() ) );
133 addChild( mRowsNode );
134 }
135 }
136 if ( entry.entry.contains( u"time"_s ) )
137 {
138 if ( !mTimeNode )
139 {
140 mTimeNode = new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr( "Execution time: %1 ms" ).arg( entry.entry.value( u"time"_s ).toString() ) );
141 addChild( mTimeNode );
142 }
143 }
144 }
145
146 QWidget *createWidget( const QgsHistoryWidgetContext & ) override
147 {
148 QgsCodeEditorSQL *editor = new QgsCodeEditorSQL();
149 editor->setText( mEntry.entry.value( u"query"_s ).toString() );
150 editor->setReadOnly( true );
151 editor->setCaretLineVisible( false );
152 editor->setLineNumbersVisible( false );
153 editor->setFoldingVisible( false );
154 editor->setEdgeMode( QsciScintilla::EdgeNone );
155 editor->setWrapMode( QsciScintilla::WrapMode::WrapWord );
156 return editor;
157 }
158
159 bool doubleClicked( const QgsHistoryWidgetContext &context ) override
160 {
161 if ( QgsDatabaseQueryHistoryWidget *queryHistoryWidget = qobject_cast< QgsDatabaseQueryHistoryWidget * >( context.historyWidget() ) )
162 {
163 queryHistoryWidget->emitSqlTriggered( mEntry.entry.value( u"connection"_s ).toString(), mEntry.entry.value( u"provider"_s ).toString(), mEntry.entry.value( u"query"_s ).toString() );
164 }
165 return true;
166 }
167
168 void populateContextMenu( QMenu *menu, const QgsHistoryWidgetContext &context ) override
169 {
170 if ( QgsDatabaseQueryHistoryWidget *queryHistoryWidget = qobject_cast< QgsDatabaseQueryHistoryWidget * >( context.historyWidget() ) )
171 {
172 QAction *loadAction = new QAction(
173 QObject::tr( "Load SQL Command…" ), menu
174 );
175 QObject::connect( loadAction, &QAction::triggered, menu, [this, queryHistoryWidget] {
176 queryHistoryWidget->emitSqlTriggered( mEntry.entry.value( u"connection"_s ).toString(), mEntry.entry.value( u"provider"_s ).toString(), mEntry.entry.value( u"query"_s ).toString() );
177 } );
178 menu->addAction( loadAction );
179 }
180
181 QAction *copyAction = new QAction(
182 QObject::tr( "Copy SQL Command" ), menu
183 );
184 copyAction->setIcon( QgsApplication::getThemeIcon( u"mActionEditCopy.svg"_s ) );
185 QObject::connect( copyAction, &QAction::triggered, menu, [this] {
186 QMimeData *m = new QMimeData();
187 m->setText( mEntry.entry.value( u"query"_s ).toString() );
188 QApplication::clipboard()->setMimeData( m );
189 } );
190 menu->addAction( copyAction );
191 }
192
193 private:
194 QString mProviderKey;
195 mutable QIcon mProviderIcon;
196 DatabaseQueryValueNode *mConnectionNode = nullptr;
197 DatabaseQueryValueNode *mRowsNode = nullptr;
198 DatabaseQueryValueNode *mTimeNode = nullptr;
199};
200
202
203
207
209{
210 return u"dbquery"_s;
211}
212
214{
215 return new DatabaseQueryRootNode( entry, this );
216}
217
219{
220 if ( DatabaseQueryRootNode *dbNode = dynamic_cast<DatabaseQueryRootNode *>( node ) )
221 {
222 dbNode->setEntry( entry );
223 }
224}
225
226//
227// QgsDatabaseQueryHistoryWidget
228//
229
231 : QgsHistoryWidget( u"dbquery"_s, backends, registry, context, parent )
232{
233}
234
235void QgsDatabaseQueryHistoryWidget::emitSqlTriggered( const QString &connectionUri, const QString &provider, const QString &sql )
236{
237 emit sqlTriggered( connectionUri, provider, sql );
238}
QFlags< HistoryProviderBackend > HistoryProviderBackends
Definition qgis.h:3579
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setText(const QString &text) override
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.
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...
QgsHistoryEntryNode * createNodeForEntry(const QgsHistoryEntry &entry, const QgsHistoryWidgetContext &context) override
Creates a new history node for the given entry.
QgsDatabaseQueryHistoryWidget(Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile, QgsHistoryProviderRegistry *registry=nullptr, const QgsHistoryWidgetContext &context=QgsHistoryWidgetContext(), QWidget *parent=nullptr)
Constructor for QgsDatabaseQueryHistoryWidget, with the specified parent widget.
void sqlTriggered(const QString &connectionUri, const QString &provider, const QString &sql)
Emitted when the user has triggered a previously executed SQL statement in the widget.
void emitSqlTriggered(const QString &connectionUri, const QString &provider, const QString &sql)
Causes the widget to emit the sqlTriggered() signal.
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.
A registry for objects which track user history (i.e.
Contains settings which reflect the context in which a history widget is shown, e....
QgsHistoryWidget * historyWidget() const
Returns the parent history widget.
QgsHistoryWidget(const QString &providerId=QString(), Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile, QgsHistoryProviderRegistry *registry=nullptr, const QgsHistoryWidgetContext &context=QgsHistoryWidgetContext(), QWidget *parent=nullptr)
Constructor for QgsHistoryWidget, with the specified parent widget.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.