33#include "moc_qgsdbqueryhistoryprovider.cpp"
35using namespace Qt::StringLiterals;
42 DatabaseQueryHistoryNode(
const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
43 : QgsHistoryEntryGroup()
45 , mProvider( provider )
49 QgsHistoryEntry mEntry;
50 QgsDatabaseQueryHistoryProvider *mProvider =
nullptr;
53class DatabaseQueryValueNode :
public DatabaseQueryHistoryNode
56 DatabaseQueryValueNode(
const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider,
const QString &value )
57 : DatabaseQueryHistoryNode( entry, provider )
61 QVariant data(
int role = Qt::DisplayRole )
const override
74 QString html(
const QgsHistoryWidgetContext & )
const override {
return mValue; }
80class DatabaseQueryRootNode :
public DatabaseQueryHistoryNode
83 DatabaseQueryRootNode(
const QgsHistoryEntry &entry, QgsDatabaseQueryHistoryProvider *provider )
84 : DatabaseQueryHistoryNode( entry, provider )
88 mProviderKey = mEntry.entry.value( u
"provider"_s ).toString();
91 QVariant data(
int role = Qt::DisplayRole )
const override
97 return mEntry.entry.value( u
"query"_s );
99 case Qt::DecorationRole:
101 if ( !mProviderIcon.isNull() )
102 return mProviderIcon;
106 mProviderIcon = md->icon();
108 return mProviderIcon;
117 void setEntry(
const QgsHistoryEntry &entry )
119 if ( !mConnectionNode )
121 mConnectionNode =
new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr(
"Connection: %1" ).arg( entry.
entry.value( u
"connection"_s ).toString() ) );
122 addChild( mConnectionNode );
124 if ( entry.
entry.contains( u
"rows"_s ) )
128 mRowsNode =
new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr(
"Row count: %1" ).arg( entry.
entry.value( u
"rows"_s ).toString() ) );
129 addChild( mRowsNode );
132 if ( entry.
entry.contains( u
"time"_s ) )
136 mTimeNode =
new DatabaseQueryValueNode( mEntry, mProvider, QObject::tr(
"Execution time: %1 ms" ).arg( entry.
entry.value( u
"time"_s ).toString() ) );
137 addChild( mTimeNode );
142 QWidget *createWidget(
const QgsHistoryWidgetContext & )
override
144 QgsCodeEditorSQL *editor =
new QgsCodeEditorSQL();
145 editor->
setText( mEntry.entry.value( u
"query"_s ).toString() );
146 editor->setReadOnly(
true );
147 editor->setCaretLineVisible(
false );
150 editor->setEdgeMode( QsciScintilla::EdgeNone );
151 editor->setWrapMode( QsciScintilla::WrapMode::WrapWord );
155 bool doubleClicked(
const QgsHistoryWidgetContext &context )
override
157 if ( QgsDatabaseQueryHistoryWidget *queryHistoryWidget = qobject_cast< QgsDatabaseQueryHistoryWidget * >( context.
historyWidget() ) )
159 queryHistoryWidget->emitSqlTriggered( mEntry.entry.value( u
"connection"_s ).toString(), mEntry.entry.value( u
"provider"_s ).toString(), mEntry.entry.value( u
"query"_s ).toString() );
164 void populateContextMenu( QMenu *menu,
const QgsHistoryWidgetContext &context )
override
166 if ( QgsDatabaseQueryHistoryWidget *queryHistoryWidget = qobject_cast< QgsDatabaseQueryHistoryWidget * >( context.
historyWidget() ) )
168 QAction *loadAction =
new QAction( QObject::tr(
"Load SQL Command…" ), menu );
169 QObject::connect( loadAction, &QAction::triggered, menu, [
this, queryHistoryWidget] {
170 queryHistoryWidget->emitSqlTriggered( mEntry.entry.value( u
"connection"_s ).toString(), mEntry.entry.value( u
"provider"_s ).toString(), mEntry.entry.value( u
"query"_s ).toString() );
172 menu->addAction( loadAction );
175 QAction *copyAction =
new QAction( QObject::tr(
"Copy SQL Command" ), menu );
177 QObject::connect( copyAction, &QAction::triggered, menu, [
this] {
178 QMimeData *m =
new QMimeData();
179 m->setText( mEntry.entry.value( u
"query"_s ).toString() );
180 QApplication::clipboard()->setMimeData( m );
182 menu->addAction( copyAction );
186 QString mProviderKey;
187 mutable QIcon mProviderIcon;
188 DatabaseQueryValueNode *mConnectionNode =
nullptr;
189 DatabaseQueryValueNode *mRowsNode =
nullptr;
190 DatabaseQueryValueNode *mTimeNode =
nullptr;
206 return new DatabaseQueryRootNode( entry,
this );
211 if ( DatabaseQueryRootNode *dbNode =
dynamic_cast<DatabaseQueryRootNode *
>( node ) )
213 dbNode->setEntry( entry );
QFlags< HistoryProviderBackend > HistoryProviderBackends
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.
QgsDatabaseQueryHistoryProvider()
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.