QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
61
62QgsDevToolsModelNode *QgsDevToolsModelGroup::addChild( std::unique_ptr<QgsDevToolsModelNode> child )
63{
64 if ( !child )
65 return nullptr;
66
67 Q_ASSERT( !child->mParent );
68 child->mParent = this;
69
70 return mChildren.emplace_back( std::move( child ) ).get();
71}
72
74{
75 Q_ASSERT( child->mParent == this );
76 auto it = std::find_if( mChildren.begin(), mChildren.end(), [&]( const std::unique_ptr<QgsDevToolsModelNode> &p ) { return p.get() == child; } );
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
129QVariant QgsDevToolsModelValueNode::data( int role ) const
130{
131 switch ( role )
132 {
133 case Qt::DisplayRole:
134 case Qt::ToolTipRole:
135 {
136 return u"%1: %2"_s.arg( mKey.leftJustified( 30, ' ' ), mValue );
137 }
138
139 case Qt::ForegroundRole:
140 {
141 if ( mColor.isValid() )
142 return QBrush( mColor );
143 break;
144 }
145 default:
146 break;
147 }
148 return QVariant();
149}
150
151QList<QAction *> QgsDevToolsModelValueNode::actions( QObject *parent )
152{
153 QList<QAction *> res;
154
155 QAction *copyAction = new QAction( QObject::tr( "Copy" ), parent );
156 QObject::connect( copyAction, &QAction::triggered, copyAction, [this] { QApplication::clipboard()->setText( u"%1: %2"_s.arg( mKey, mValue ) ); } );
157
158 res << copyAction;
159
160 return res;
161}
162
163//
164// QgsDevToolsModelGroup
165//
166
167void QgsDevToolsModelGroup::addKeyValueNode( const QString &key, const QString &value, const QColor &color )
168{
169 addChild( std::make_unique<QgsDevToolsModelValueNode>( key, value, color ) );
170}
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.