Skip to content

Commit 1827546

Browse files
committed
Add new properties short name and show in attribute table to actions
1 parent 54ec3b4 commit 1827546

15 files changed

+386
-246
lines changed

src/app/qgsattributeactiondialog.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,22 @@ void QgsAttributeActionDialog::insertRow( int row, const QgsAction& action )
9595
item = new QTableWidgetItem( textForType( action.type() ) );
9696
item->setData( Qt::UserRole, action.type() );
9797
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
98-
mAttributeActionTable->setItem( row, 0, item );
98+
mAttributeActionTable->setItem( row, Type, item );
9999

100-
// Name
101-
mAttributeActionTable->setItem( row, 1, new QTableWidgetItem( action.name() ) );
100+
// Description
101+
mAttributeActionTable->setItem( row, Description, new QTableWidgetItem( action.name() ) );
102+
103+
// Short Title
104+
mAttributeActionTable->setItem( row, ShortTitle, new QTableWidgetItem( action.shortTitle() ) );
102105

103106
// Action text
104-
mAttributeActionTable->setItem( row, 2, new QTableWidgetItem( action.action() ) );
107+
mAttributeActionTable->setItem( row, ActionText, new QTableWidgetItem( action.action() ) );
105108

106109
// Capture output
107110
item = new QTableWidgetItem();
108-
item->setFlags( item->flags() & ~( Qt::ItemIsEditable | Qt::ItemIsUserCheckable ) );
111+
item->setFlags( item->flags() & ~( Qt::ItemIsEditable ) );
109112
item->setCheckState( action.capture() ? Qt::Checked : Qt::Unchecked );
110-
mAttributeActionTable->setItem( row, 3, item );
113+
mAttributeActionTable->setItem( row, Capture, item );
111114

112115
// Icon
113116
QIcon icon = action.icon();
@@ -182,11 +185,12 @@ void QgsAttributeActionDialog::swapRows( int row1, int row2 )
182185

183186
QgsAction QgsAttributeActionDialog::rowToAction( int row ) const
184187
{
185-
QgsAction action( static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, 0 )->data( Qt::UserRole ).toInt() ),
186-
mAttributeActionTable->item( row, 1 )->text(),
187-
mAttributeActionTable->item( row, 2 )->text(),
188+
QgsAction action( static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, Type )->data( Qt::UserRole ).toInt() ),
189+
mAttributeActionTable->item( row, Description )->text(),
190+
mAttributeActionTable->item( row, ActionText )->text(),
188191
mAttributeActionTable->verticalHeaderItem( row )->data( Qt::UserRole ).toString(),
189-
mAttributeActionTable->item( row, 3 )->checkState() == Qt::Checked );
192+
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
193+
mAttributeActionTable->item( row, ShortTitle )->text() );
190194
return action;
191195
}
192196

@@ -237,7 +241,7 @@ void QgsAttributeActionDialog::insert()
237241

238242
if ( dlg.exec() )
239243
{
240-
QString name = uniqueName( dlg.name() );
244+
QString name = uniqueName( dlg.description() );
241245

242246
insertRow( pos, dlg.type(), name, dlg.actionText(), dlg.iconPath(), dlg.capture() );
243247
}
@@ -280,23 +284,25 @@ void QgsAttributeActionDialog::itemDoubleClicked( QTableWidgetItem* item )
280284
int row = item->row();
281285

282286
QgsAttributeActionPropertiesDialog actionProperties(
283-
static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, 0 )->data( Qt::UserRole ).toInt() ),
284-
mAttributeActionTable->item( row, 1 )->text(),
287+
static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, Type )->data( Qt::UserRole ).toInt() ),
288+
mAttributeActionTable->item( row, Description )->text(),
289+
mAttributeActionTable->item( row, ShortTitle )->text(),
285290
mAttributeActionTable->verticalHeaderItem( row )->data( Qt::UserRole ).toString(),
286-
mAttributeActionTable->item( row, 2 )->text(),
287-
mAttributeActionTable->item( row, 3 )->checkState() == Qt::Checked,
291+
mAttributeActionTable->item( row, ActionText )->text(),
292+
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
288293
mLayer
289294
);
290295

291296
actionProperties.setWindowTitle( tr( "Edit action" ) );
292297

293298
if ( actionProperties.exec() )
294299
{
295-
mAttributeActionTable->item( row, 0 )->setData( Qt::UserRole, actionProperties.type() );
296-
mAttributeActionTable->item( row, 0 )->setText( textForType( actionProperties.type() ) );
297-
mAttributeActionTable->item( row, 1 )->setText( actionProperties.name() );
298-
mAttributeActionTable->item( row, 2 )->setText( actionProperties.actionText() );
299-
mAttributeActionTable->item( row, 3 )->setCheckState( actionProperties.capture() ? Qt::Checked : Qt::Unchecked );
300+
mAttributeActionTable->item( row, Type )->setData( Qt::UserRole, actionProperties.type() );
301+
mAttributeActionTable->item( row, Type )->setText( textForType( actionProperties.type() ) );
302+
mAttributeActionTable->item( row, Description )->setText( actionProperties.description() );
303+
mAttributeActionTable->item( row, ShortTitle )->setText( actionProperties.shortTitle() );
304+
mAttributeActionTable->item( row, ActionText )->setText( actionProperties.actionText() );
305+
mAttributeActionTable->item( row, Capture )->setCheckState( actionProperties.capture() ? Qt::Checked : Qt::Unchecked );
300306
mAttributeActionTable->verticalHeaderItem( row )->setData( Qt::UserRole, actionProperties.iconPath() );
301307
mAttributeActionTable->verticalHeaderItem( row )->setIcon( QIcon( actionProperties.iconPath() ) );
302308
}

src/app/qgsattributeactiondialog.h

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ back to QgsVectorLayer.
3131
class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttributeActionDialogBase
3232
{
3333
Q_OBJECT
34+
private:
35+
enum ColumnIndexes
36+
{
37+
Type,
38+
Description,
39+
ShortTitle,
40+
ActionText,
41+
Capture
42+
};
3443

3544
public:
3645
QgsAttributeActionDialog( const QgsActionManager& actions,

src/app/qgsattributeactionpropertiesdialog.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
#include <QFileDialog>
2828
#include <QImageWriter>
2929

30-
QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString& name, const QString& iconPath, const QString& actionText, bool capture, QgsVectorLayer* layer, QWidget* parent )
30+
QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString& description, const QString& shortTitle, const QString& iconPath, const QString& actionText, bool capture, QgsVectorLayer* layer, QWidget* parent )
3131
: QDialog( parent )
3232
, mLayer( layer )
3333
{
3434
setupUi( this );
3535

3636
mActionType->setCurrentIndex( type );
37-
mActionName->setText( name );
37+
mActionName->setText( description );
38+
mShortTitle->setText( shortTitle );
3839
mActionIcon->setText( iconPath );
3940
mIconPreview->setPixmap( QPixmap( iconPath ) );
40-
mActionText->setPlainText( actionText );
41+
mActionText->setText( actionText );
4142
mCaptureOutput->setChecked( capture );
4243

4344
// display the expression builder
@@ -96,19 +97,24 @@ QgsAction::ActionType QgsAttributeActionPropertiesDialog::type() const
9697
return static_cast<QgsAction::ActionType>( mActionType->currentIndex() );
9798
}
9899

99-
QString QgsAttributeActionPropertiesDialog::name() const
100+
QString QgsAttributeActionPropertiesDialog::description() const
100101
{
101102
return mActionName->text();
102103
}
103104

105+
QString QgsAttributeActionPropertiesDialog::shortTitle() const
106+
{
107+
return mShortTitle->text();
108+
}
109+
104110
QString QgsAttributeActionPropertiesDialog::iconPath() const
105111
{
106112
return mActionIcon->text();
107113
}
108114

109115
QString QgsAttributeActionPropertiesDialog::actionText() const
110116
{
111-
return mActionText->toPlainText();
117+
return mActionText->text();
112118
}
113119

114120
bool QgsAttributeActionPropertiesDialog::capture() const
@@ -123,18 +129,18 @@ void QgsAttributeActionPropertiesDialog::browse()
123129
this, tr( "Select an action", "File dialog window title" ), QDir::homePath() );
124130

125131
if ( !action.isNull() )
126-
mActionText->insertPlainText( action );
132+
mActionText->insertText( action );
127133
}
128134

129135
void QgsAttributeActionPropertiesDialog::insertExpressionOrField()
130136
{
131-
QString selText = mActionText->textCursor().selectedText();
137+
QString selText = mActionText->selectedText();
132138

133139
// edit the selected expression if there's one
134140
if ( selText.startsWith( "[%" ) && selText.endsWith( "%]" ) )
135141
selText = selText.mid( 2, selText.size() - 4 );
136142

137-
mActionText->insertPlainText( "[%" + mFieldExpression->currentField() + "%]" );
143+
mActionText->insertText( "[%" + mFieldExpression->currentField() + "%]" );
138144
}
139145

140146
void QgsAttributeActionPropertiesDialog::chooseIcon()
@@ -156,7 +162,7 @@ void QgsAttributeActionPropertiesDialog::chooseIcon()
156162

157163
void QgsAttributeActionPropertiesDialog::updateButtons()
158164
{
159-
if ( mActionName->text().isEmpty() || mActionText->toPlainText().isEmpty() )
165+
if ( mActionName->text().isEmpty() || mActionText->text().isEmpty() )
160166
{
161167
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
162168
}

src/app/qgsattributeactionpropertiesdialog.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ class QgsAttributeActionPropertiesDialog: public QDialog, private Ui::QgsAttribu
2727
Q_OBJECT
2828

2929
public:
30-
QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString& name, const QString& iconPath, const QString& actionText, bool capture, QgsVectorLayer* layer, QWidget* parent = nullptr );
30+
QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString& description, const QString& shortTitle, const QString& iconPath, const QString& actionText, bool capture, QgsVectorLayer* layer, QWidget* parent = nullptr );
3131

3232
QgsAttributeActionPropertiesDialog( QgsVectorLayer* layer, QWidget* parent = nullptr );
3333

3434
QgsAction::ActionType type() const;
3535

36-
QString name() const;
36+
QString description() const;
37+
38+
QString shortTitle() const;
3739

3840
QString iconPath() const;
3941

src/core/qgsaction.h

+17-8
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,27 @@ class CORE_EXPORT QgsAction
3535
OpenUrl,
3636
};
3737

38-
QgsAction( ActionType type, const QString& name, const QString& action, bool capture )
38+
QgsAction( ActionType type, const QString& description, const QString& action, bool capture )
3939
: mType( type )
40-
, mName( name )
40+
, mDescription( description )
4141
, mAction( action )
4242
, mCaptureOutput( capture )
4343
{}
4444

45-
QgsAction( ActionType type, const QString& name, const QString& action, const QString& icon, bool capture )
45+
QgsAction( ActionType type, const QString& description, const QString& action, const QString& icon, bool capture, const QString& shortTitle = QString(), bool showInAttributeTable = true )
4646
: mType( type )
47-
, mName( name )
47+
, mDescription( description )
48+
, mShortTitle( shortTitle )
4849
, mIcon( icon )
4950
, mAction( action )
5051
, mCaptureOutput( capture )
52+
, mShowInAttributeTable( showInAttributeTable )
5153
{}
5254

53-
//! The name of the action
54-
QString name() const { return mName; }
55+
//! The name of the action. This may be a longer description.
56+
QString name() const { return mDescription; }
57+
58+
QString shortTitle() const { return mShortTitle; }
5559

5660
//! The path to the icon
5761
QString iconPath() const { return mIcon; }
@@ -68,15 +72,20 @@ class CORE_EXPORT QgsAction
6872
//! Whether to capture output for display when this action is run
6973
bool capture() const { return mCaptureOutput; }
7074

71-
//! Whether the action is runable on the current platform
75+
//! Returns true if the action should be shown on the attribute table
76+
bool showInAttributeTable() const { return mShowInAttributeTable; }
77+
78+
//! Checks if the action is runable on the current platform
7279
bool runable() const;
7380

7481
private:
7582
ActionType mType;
76-
QString mName;
83+
QString mDescription;
84+
QString mShortTitle;
7785
QString mIcon;
7886
QString mAction;
7987
bool mCaptureOutput;
88+
bool mShowInAttributeTable;
8089
};
8190

8291
#endif // QGSACTION_H

src/core/qgsactionmanager.cpp

+17-11
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,16 @@ bool QgsActionManager::writeXML( QDomNode& layer_node, QDomDocument& doc ) const
275275
{
276276
QDomElement aActions = doc.createElement( "attributeactions" );
277277

278-
for ( int i = 0; i < mActions.size(); i++ )
278+
Q_FOREACH ( const QgsAction& action, mActions )
279279
{
280280
QDomElement actionSetting = doc.createElement( "actionsetting" );
281-
actionSetting.setAttribute( "type", mActions[i].type() );
282-
actionSetting.setAttribute( "name", mActions[i].name() );
283-
actionSetting.setAttribute( "icon", mActions[i].iconPath() );
284-
actionSetting.setAttribute( "action", mActions[i].action() );
285-
actionSetting.setAttribute( "capture", mActions[i].capture() );
281+
actionSetting.setAttribute( "type", action.type() );
282+
actionSetting.setAttribute( "name", action.name() );
283+
actionSetting.setAttribute( "shortTitle", action.shortTitle() );
284+
actionSetting.setAttribute( "icon", action.iconPath() );
285+
actionSetting.setAttribute( "action", action.action() );
286+
actionSetting.setAttribute( "showInAttributeTable", action.showInAttributeTable() );
287+
actionSetting.setAttribute( "capture", action.capture() );
286288
aActions.appendChild( actionSetting );
287289
}
288290
layer_node.appendChild( aActions );
@@ -302,11 +304,15 @@ bool QgsActionManager::readXML( const QDomNode& layer_node )
302304
for ( int i = 0; i < actionsettings.size(); ++i )
303305
{
304306
QDomElement setting = actionsettings.item( i ).toElement();
305-
addAction( static_cast< QgsAction::ActionType >( setting.attributeNode( "type" ).value().toInt() ),
306-
setting.attributeNode( "name" ).value(),
307-
setting.attributeNode( "action" ).value(),
308-
setting.attributeNode( "icon" ).value(),
309-
setting.attributeNode( "capture" ).value().toInt() != 0 );
307+
mActions.append(
308+
QgsAction( static_cast< QgsAction::ActionType >( setting.attributeNode( "type" ).value().toInt() ),
309+
setting.attributeNode( "name" ).value(),
310+
setting.attributeNode( "action" ).value(),
311+
setting.attributeNode( "icon" ).value(),
312+
setting.attributeNode( "capture" ).value().toInt() != 0,
313+
setting.attributeNode( "shortTitle" ).value(),
314+
setting.attributeNode( "showInAttributeTable" ).value().toInt() != 0 )
315+
);
310316
}
311317
}
312318
return true;

src/core/qgsactionmanager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class CORE_EXPORT QgsActionManager
138138
/**
139139
* Get the action at the specified index.
140140
*/
141-
QgsAction at( int idx ) const { return mActions.at( idx ); }
141+
const QgsAction& at( int idx ) const { return mActions.at( idx ); }
142142

143143
/**
144144
* Get the action at the specified index.

src/gui/attributetable/qgsattributetabledelegate.cpp

+21-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <QLineEdit>
1818
#include <QComboBox>
1919
#include <QPainter>
20+
#include <QToolButton>
2021

2122
#include "qgsattributeeditor.h"
2223
#include "qgsattributetabledelegate.h"
@@ -27,6 +28,7 @@
2728
#include "qgsfeatureselectionmodel.h"
2829
#include "qgslogger.h"
2930
#include "qgsvectordataprovider.h"
31+
#include "qgsactionmanager.h"
3032

3133

3234
QgsVectorLayer* QgsAttributeTableDelegate::layer( const QAbstractItemModel *model )
@@ -55,10 +57,7 @@ const QgsAttributeTableModel* QgsAttributeTableDelegate::masterModel( const QAbs
5557
return nullptr;
5658
}
5759

58-
QWidget *QgsAttributeTableDelegate::createEditor(
59-
QWidget *parent,
60-
const QStyleOptionViewItem &option,
61-
const QModelIndex &index ) const
60+
QWidget* QgsAttributeTableDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
6261
{
6362
Q_UNUSED( option );
6463
QgsVectorLayer *vl = layer( index.model() );
@@ -119,9 +118,8 @@ void QgsAttributeTableDelegate::setFeatureSelectionModel( QgsFeatureSelectionMod
119118
mFeatureSelectionModel = featureSelectionModel;
120119
}
121120

122-
void QgsAttributeTableDelegate::paint( QPainter * painter,
123-
const QStyleOptionViewItem & option,
124-
const QModelIndex & index ) const
121+
122+
void QgsAttributeTableDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
125123
{
126124
QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
127125

@@ -136,15 +134,23 @@ void QgsAttributeTableDelegate::paint( QPainter * painter,
136134
if ( mFeatureSelectionModel && mFeatureSelectionModel->isSelected( fid ) )
137135
myOpt.state |= QStyle::State_Selected;
138136

139-
QItemDelegate::paint( painter, myOpt, index );
140137

141-
if ( option.state & QStyle::State_HasFocus )
138+
if ( index.column() == 0 )
139+
{
140+
painter->drawImage( QPoint( 0, 0 ), mActionButtonImage );
141+
}
142+
else
142143
{
143-
QRect r = option.rect.adjusted( 1, 1, -1, -1 );
144-
QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
145-
painter->save();
146-
painter->setPen( p );
147-
painter->drawRect( r );
148-
painter->restore();
144+
QItemDelegate::paint( painter, myOpt, index );
145+
146+
if ( option.state & QStyle::State_HasFocus )
147+
{
148+
QRect r = option.rect.adjusted( 1, 1, -1, -1 );
149+
QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
150+
painter->save();
151+
painter->setPen( p );
152+
painter->drawRect( r );
153+
painter->restore();
154+
}
149155
}
150156
}

0 commit comments

Comments
 (0)