QGIS API Documentation 3.99.0-Master (357b655ed83)
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 <QString>
30#include <QUrlQuery>
31
32using namespace Qt::StringLiterals;
33
34//
35// QgsDevToolsModelNode
36//
37
40
42{
43 return QVariant();
44}
45
46QList<QAction *> QgsDevToolsModelNode::actions( QObject * )
47{
48 return QList<QAction *>();
49}
50
51
52//
53// QgsDevToolsModelGroup
54//
55
57 : mGroupTitle( title )
58{
59}
60
62
63QgsDevToolsModelNode *QgsDevToolsModelGroup::addChild( std::unique_ptr<QgsDevToolsModelNode> child )
64{
65 if ( !child )
66 return nullptr;
67
68 Q_ASSERT( !child->mParent );
69 child->mParent = this;
70
71 return mChildren.emplace_back( std::move( child ) ).get();
72}
73
75{
76 Q_ASSERT( child->mParent == this );
77 auto it = std::find_if( mChildren.begin(), mChildren.end(), [&]( const std::unique_ptr<QgsDevToolsModelNode> &p ) {
78 return p.get() == child;
79 } );
80 if ( it != mChildren.end() )
81 return std::distance( mChildren.begin(), it );
82 return -1;
83}
84
86{
87 Q_ASSERT( static_cast<std::size_t>( index ) < mChildren.size() );
88 return mChildren[index].get();
89}
90
92{
93 mChildren.clear();
94}
95
96QVariant QgsDevToolsModelGroup::data( int role ) const
97{
98 switch ( role )
99 {
100 case Qt::DisplayRole:
101 return mGroupTitle;
102
103 default:
104 break;
105 }
106 return QVariant();
107}
108
110{
111 QVariantMap res;
112 for ( const std::unique_ptr<QgsDevToolsModelNode> &child : mChildren )
113 {
114 if ( const QgsDevToolsModelValueNode *valueNode = dynamic_cast<const QgsDevToolsModelValueNode *>( child.get() ) )
115 {
116 res.insert( valueNode->key(), valueNode->value() );
117 }
118 }
119 return res;
120}
121
122
123//
124// QgsDevToolsModelValueNode
125//
126QgsDevToolsModelValueNode::QgsDevToolsModelValueNode( const QString &key, const QString &value, const QColor &color )
127 : mKey( key )
128 , mValue( value )
129 , mColor( color )
130{
131}
132
133QVariant QgsDevToolsModelValueNode::data( int role ) const
134{
135 switch ( role )
136 {
137 case Qt::DisplayRole:
138 case Qt::ToolTipRole:
139 {
140 return u"%1: %2"_s.arg( mKey.leftJustified( 30, ' ' ), mValue );
141 }
142
143 case Qt::ForegroundRole:
144 {
145 if ( mColor.isValid() )
146 return QBrush( mColor );
147 break;
148 }
149 default:
150 break;
151 }
152 return QVariant();
153}
154
155QList<QAction *> QgsDevToolsModelValueNode::actions( QObject *parent )
156{
157 QList<QAction *> res;
158
159 QAction *copyAction = new QAction( QObject::tr( "Copy" ), parent );
160 QObject::connect( copyAction, &QAction::triggered, copyAction, [this] {
161 QApplication::clipboard()->setText( u"%1: %2"_s.arg( mKey, mValue ) );
162 } );
163
164 res << copyAction;
165
166 return res;
167}
168
169//
170// QgsDevToolsModelGroup
171//
172
173void QgsDevToolsModelGroup::addKeyValueNode( const QString &key, const QString &value, const QColor &color )
174{
175 addChild( std::make_unique<QgsDevToolsModelValueNode>( key, value, color ) );
176}
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.