QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsdevtoolsmodelnode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdevtoolsmodelnode.cpp
3 -------------------------
4 begin : March 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <nlohmann/json.hpp>
19
20#include "qgis.h"
21
22#include <QAction>
23#include <QApplication>
24#include <QBrush>
25#include <QClipboard>
26#include <QColor>
27#include <QDesktopServices>
28#include <QFont>
29#include <QUrlQuery>
30
31//
32// QgsDevToolsModelNode
33//
34
37
39{
40 return QVariant();
41}
42
43QList<QAction *> QgsDevToolsModelNode::actions( QObject * )
44{
45 return QList<QAction *>();
46}
47
48
49//
50// QgsDevToolsModelGroup
51//
52
54 : mGroupTitle( title )
55{
56}
57
59
60QgsDevToolsModelNode *QgsDevToolsModelGroup::addChild( std::unique_ptr<QgsDevToolsModelNode> child )
61{
62 if ( !child )
63 return nullptr;
64
65 Q_ASSERT( !child->mParent );
66 child->mParent = this;
67
68 return mChildren.emplace_back( std::move( child ) ).get();
69}
70
72{
73 Q_ASSERT( child->mParent == this );
74 auto it = std::find_if( mChildren.begin(), mChildren.end(), [&]( const std::unique_ptr<QgsDevToolsModelNode> &p ) {
75 return p.get() == child;
76 } );
77 if ( it != mChildren.end() )
78 return std::distance( mChildren.begin(), it );
79 return -1;
80}
81
83{
84 Q_ASSERT( static_cast<std::size_t>( index ) < mChildren.size() );
85 return mChildren[index].get();
86}
87
89{
90 mChildren.clear();
91}
92
93QVariant QgsDevToolsModelGroup::data( int role ) const
94{
95 switch ( role )
96 {
97 case Qt::DisplayRole:
98 return mGroupTitle;
99
100 default:
101 break;
102 }
103 return QVariant();
104}
105
107{
108 QVariantMap res;
109 for ( const std::unique_ptr<QgsDevToolsModelNode> &child : mChildren )
110 {
111 if ( const QgsDevToolsModelValueNode *valueNode = dynamic_cast<const QgsDevToolsModelValueNode *>( child.get() ) )
112 {
113 res.insert( valueNode->key(), valueNode->value() );
114 }
115 }
116 return res;
117}
118
119
120//
121// QgsDevToolsModelValueNode
122//
123QgsDevToolsModelValueNode::QgsDevToolsModelValueNode( const QString &key, const QString &value, const QColor &color )
124 : mKey( key )
125 , mValue( value )
126 , mColor( color )
127{
128}
129
130QVariant QgsDevToolsModelValueNode::data( int role ) const
131{
132 switch ( role )
133 {
134 case Qt::DisplayRole:
135 case Qt::ToolTipRole:
136 {
137 return QStringLiteral( "%1: %2" ).arg( mKey.leftJustified( 30, ' ' ), mValue );
138 }
139
140 case Qt::ForegroundRole:
141 {
142 if ( mColor.isValid() )
143 return QBrush( mColor );
144 break;
145 }
146 default:
147 break;
148 }
149 return QVariant();
150}
151
152QList<QAction *> QgsDevToolsModelValueNode::actions( QObject *parent )
153{
154 QList<QAction *> res;
155
156 QAction *copyAction = new QAction( QObject::tr( "Copy" ), parent );
157 QObject::connect( copyAction, &QAction::triggered, copyAction, [this] {
158 QApplication::clipboard()->setText( QStringLiteral( "%1: %2" ).arg( mKey, mValue ) );
159 } );
160
161 res << copyAction;
162
163 return res;
164}
165
166//
167// QgsDevToolsModelGroup
168//
169
170void QgsDevToolsModelGroup::addKeyValueNode( const QString &key, const QString &value, const QColor &color )
171{
172 addChild( std::make_unique<QgsDevToolsModelValueNode>( key, value, color ) );
173}
void addKeyValueNode(const QString &key, const QString &value, const QColor &color=QColor())
Adds a simple key: value node to the group.
QgsDevToolsModelNode * addChild(std::unique_ptr< QgsDevToolsModelNode > child)
Adds a child node to this node.
~QgsDevToolsModelGroup() override
void clear()
Clears the group, removing all its children.
QVariant data(int role=Qt::DisplayRole) const override
Returns the node's data for the specified model role.
std::deque< std::unique_ptr< QgsDevToolsModelNode > > mChildren
QgsDevToolsModelNode * childAt(int index)
Returns the child at the specified index.
QVariant toVariant() const override
Converts the node's contents to a variant.
int indexOf(QgsDevToolsModelNode *child) const
Returns the index of the specified child node.
virtual QList< QAction * > actions(QObject *parent)
Returns a list of actions relating to the node.
virtual ~QgsDevToolsModelNode()
virtual QVariant toVariant() const
Converts the node's contents to a variant.
QgsDevToolsModelGroup * parent()
Returns the node's parent node.
A "key: value" style node for a dev tools model.
QVariant data(int role=Qt::DisplayRole) const final
Returns the node's data for the specified model role.
QgsDevToolsModelValueNode(const QString &key, const QString &value, const QColor &color=QColor())
Constructor for QgsDevToolsModelValueNode, with the specified key (usually translated) and value.
QString key() const
Returns the node's key.
QString value() const
Returns the node's value.
QList< QAction * > actions(QObject *parent) final
Returns a list of actions relating to the node.