Skip to content

Commit

Permalink
Add new properties short name and show in attribute table to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 2, 2016
1 parent 54ec3b4 commit 1827546
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 246 deletions.
46 changes: 26 additions & 20 deletions src/app/qgsattributeactiondialog.cpp
Expand Up @@ -95,19 +95,22 @@ void QgsAttributeActionDialog::insertRow( int row, const QgsAction& action )
item = new QTableWidgetItem( textForType( action.type() ) );
item->setData( Qt::UserRole, action.type() );
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
mAttributeActionTable->setItem( row, 0, item );
mAttributeActionTable->setItem( row, Type, item );

// Name
mAttributeActionTable->setItem( row, 1, new QTableWidgetItem( action.name() ) );
// Description
mAttributeActionTable->setItem( row, Description, new QTableWidgetItem( action.name() ) );

// Short Title
mAttributeActionTable->setItem( row, ShortTitle, new QTableWidgetItem( action.shortTitle() ) );

// Action text
mAttributeActionTable->setItem( row, 2, new QTableWidgetItem( action.action() ) );
mAttributeActionTable->setItem( row, ActionText, new QTableWidgetItem( action.action() ) );

// Capture output
item = new QTableWidgetItem();
item->setFlags( item->flags() & ~( Qt::ItemIsEditable | Qt::ItemIsUserCheckable ) );
item->setFlags( item->flags() & ~( Qt::ItemIsEditable ) );
item->setCheckState( action.capture() ? Qt::Checked : Qt::Unchecked );
mAttributeActionTable->setItem( row, 3, item );
mAttributeActionTable->setItem( row, Capture, item );

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

QgsAction QgsAttributeActionDialog::rowToAction( int row ) const
{
QgsAction action( static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, 0 )->data( Qt::UserRole ).toInt() ),
mAttributeActionTable->item( row, 1 )->text(),
mAttributeActionTable->item( row, 2 )->text(),
QgsAction action( static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, Type )->data( Qt::UserRole ).toInt() ),
mAttributeActionTable->item( row, Description )->text(),
mAttributeActionTable->item( row, ActionText )->text(),
mAttributeActionTable->verticalHeaderItem( row )->data( Qt::UserRole ).toString(),
mAttributeActionTable->item( row, 3 )->checkState() == Qt::Checked );
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
mAttributeActionTable->item( row, ShortTitle )->text() );
return action;
}

Expand Down Expand Up @@ -237,7 +241,7 @@ void QgsAttributeActionDialog::insert()

if ( dlg.exec() )
{
QString name = uniqueName( dlg.name() );
QString name = uniqueName( dlg.description() );

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

QgsAttributeActionPropertiesDialog actionProperties(
static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, 0 )->data( Qt::UserRole ).toInt() ),
mAttributeActionTable->item( row, 1 )->text(),
static_cast<QgsAction::ActionType>( mAttributeActionTable->item( row, Type )->data( Qt::UserRole ).toInt() ),
mAttributeActionTable->item( row, Description )->text(),
mAttributeActionTable->item( row, ShortTitle )->text(),
mAttributeActionTable->verticalHeaderItem( row )->data( Qt::UserRole ).toString(),
mAttributeActionTable->item( row, 2 )->text(),
mAttributeActionTable->item( row, 3 )->checkState() == Qt::Checked,
mAttributeActionTable->item( row, ActionText )->text(),
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
mLayer
);

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

if ( actionProperties.exec() )
{
mAttributeActionTable->item( row, 0 )->setData( Qt::UserRole, actionProperties.type() );
mAttributeActionTable->item( row, 0 )->setText( textForType( actionProperties.type() ) );
mAttributeActionTable->item( row, 1 )->setText( actionProperties.name() );
mAttributeActionTable->item( row, 2 )->setText( actionProperties.actionText() );
mAttributeActionTable->item( row, 3 )->setCheckState( actionProperties.capture() ? Qt::Checked : Qt::Unchecked );
mAttributeActionTable->item( row, Type )->setData( Qt::UserRole, actionProperties.type() );
mAttributeActionTable->item( row, Type )->setText( textForType( actionProperties.type() ) );
mAttributeActionTable->item( row, Description )->setText( actionProperties.description() );
mAttributeActionTable->item( row, ShortTitle )->setText( actionProperties.shortTitle() );
mAttributeActionTable->item( row, ActionText )->setText( actionProperties.actionText() );
mAttributeActionTable->item( row, Capture )->setCheckState( actionProperties.capture() ? Qt::Checked : Qt::Unchecked );
mAttributeActionTable->verticalHeaderItem( row )->setData( Qt::UserRole, actionProperties.iconPath() );
mAttributeActionTable->verticalHeaderItem( row )->setIcon( QIcon( actionProperties.iconPath() ) );
}
Expand Down
9 changes: 9 additions & 0 deletions src/app/qgsattributeactiondialog.h
Expand Up @@ -31,6 +31,15 @@ back to QgsVectorLayer.
class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttributeActionDialogBase
{
Q_OBJECT
private:
enum ColumnIndexes
{
Type,
Description,
ShortTitle,
ActionText,
Capture
};

public:
QgsAttributeActionDialog( const QgsActionManager& actions,
Expand Down
24 changes: 15 additions & 9 deletions src/app/qgsattributeactionpropertiesdialog.cpp
Expand Up @@ -27,17 +27,18 @@
#include <QFileDialog>
#include <QImageWriter>

QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString& name, const QString& iconPath, const QString& actionText, bool capture, QgsVectorLayer* layer, QWidget* parent )
QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString& description, const QString& shortTitle, const QString& iconPath, const QString& actionText, bool capture, QgsVectorLayer* layer, QWidget* parent )
: QDialog( parent )
, mLayer( layer )
{
setupUi( this );

mActionType->setCurrentIndex( type );
mActionName->setText( name );
mActionName->setText( description );
mShortTitle->setText( shortTitle );
mActionIcon->setText( iconPath );
mIconPreview->setPixmap( QPixmap( iconPath ) );
mActionText->setPlainText( actionText );
mActionText->setText( actionText );
mCaptureOutput->setChecked( capture );

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

QString QgsAttributeActionPropertiesDialog::name() const
QString QgsAttributeActionPropertiesDialog::description() const
{
return mActionName->text();
}

QString QgsAttributeActionPropertiesDialog::shortTitle() const
{
return mShortTitle->text();
}

QString QgsAttributeActionPropertiesDialog::iconPath() const
{
return mActionIcon->text();
}

QString QgsAttributeActionPropertiesDialog::actionText() const
{
return mActionText->toPlainText();
return mActionText->text();
}

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

if ( !action.isNull() )
mActionText->insertPlainText( action );
mActionText->insertText( action );
}

void QgsAttributeActionPropertiesDialog::insertExpressionOrField()
{
QString selText = mActionText->textCursor().selectedText();
QString selText = mActionText->selectedText();

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

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

void QgsAttributeActionPropertiesDialog::chooseIcon()
Expand All @@ -156,7 +162,7 @@ void QgsAttributeActionPropertiesDialog::chooseIcon()

void QgsAttributeActionPropertiesDialog::updateButtons()
{
if ( mActionName->text().isEmpty() || mActionText->toPlainText().isEmpty() )
if ( mActionName->text().isEmpty() || mActionText->text().isEmpty() )
{
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/qgsattributeactionpropertiesdialog.h
Expand Up @@ -27,13 +27,15 @@ class QgsAttributeActionPropertiesDialog: public QDialog, private Ui::QgsAttribu
Q_OBJECT

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

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

QgsAction::ActionType type() const;

QString name() const;
QString description() const;

QString shortTitle() const;

QString iconPath() const;

Expand Down
25 changes: 17 additions & 8 deletions src/core/qgsaction.h
Expand Up @@ -35,23 +35,27 @@ class CORE_EXPORT QgsAction
OpenUrl,
};

QgsAction( ActionType type, const QString& name, const QString& action, bool capture )
QgsAction( ActionType type, const QString& description, const QString& action, bool capture )
: mType( type )
, mName( name )
, mDescription( description )
, mAction( action )
, mCaptureOutput( capture )
{}

QgsAction( ActionType type, const QString& name, const QString& action, const QString& icon, bool capture )
QgsAction( ActionType type, const QString& description, const QString& action, const QString& icon, bool capture, const QString& shortTitle = QString(), bool showInAttributeTable = true )
: mType( type )
, mName( name )
, mDescription( description )
, mShortTitle( shortTitle )
, mIcon( icon )
, mAction( action )
, mCaptureOutput( capture )
, mShowInAttributeTable( showInAttributeTable )
{}

//! The name of the action
QString name() const { return mName; }
//! The name of the action. This may be a longer description.
QString name() const { return mDescription; }

QString shortTitle() const { return mShortTitle; }

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

//! Whether the action is runable on the current platform
//! Returns true if the action should be shown on the attribute table
bool showInAttributeTable() const { return mShowInAttributeTable; }

//! Checks if the action is runable on the current platform
bool runable() const;

private:
ActionType mType;
QString mName;
QString mDescription;
QString mShortTitle;
QString mIcon;
QString mAction;
bool mCaptureOutput;
bool mShowInAttributeTable;
};

#endif // QGSACTION_H
28 changes: 17 additions & 11 deletions src/core/qgsactionmanager.cpp
Expand Up @@ -275,14 +275,16 @@ bool QgsActionManager::writeXML( QDomNode& layer_node, QDomDocument& doc ) const
{
QDomElement aActions = doc.createElement( "attributeactions" );

for ( int i = 0; i < mActions.size(); i++ )
Q_FOREACH ( const QgsAction& action, mActions )
{
QDomElement actionSetting = doc.createElement( "actionsetting" );
actionSetting.setAttribute( "type", mActions[i].type() );
actionSetting.setAttribute( "name", mActions[i].name() );
actionSetting.setAttribute( "icon", mActions[i].iconPath() );
actionSetting.setAttribute( "action", mActions[i].action() );
actionSetting.setAttribute( "capture", mActions[i].capture() );
actionSetting.setAttribute( "type", action.type() );
actionSetting.setAttribute( "name", action.name() );
actionSetting.setAttribute( "shortTitle", action.shortTitle() );
actionSetting.setAttribute( "icon", action.iconPath() );
actionSetting.setAttribute( "action", action.action() );
actionSetting.setAttribute( "showInAttributeTable", action.showInAttributeTable() );
actionSetting.setAttribute( "capture", action.capture() );
aActions.appendChild( actionSetting );
}
layer_node.appendChild( aActions );
Expand All @@ -302,11 +304,15 @@ bool QgsActionManager::readXML( const QDomNode& layer_node )
for ( int i = 0; i < actionsettings.size(); ++i )
{
QDomElement setting = actionsettings.item( i ).toElement();
addAction( static_cast< QgsAction::ActionType >( setting.attributeNode( "type" ).value().toInt() ),
setting.attributeNode( "name" ).value(),
setting.attributeNode( "action" ).value(),
setting.attributeNode( "icon" ).value(),
setting.attributeNode( "capture" ).value().toInt() != 0 );
mActions.append(
QgsAction( static_cast< QgsAction::ActionType >( setting.attributeNode( "type" ).value().toInt() ),
setting.attributeNode( "name" ).value(),
setting.attributeNode( "action" ).value(),
setting.attributeNode( "icon" ).value(),
setting.attributeNode( "capture" ).value().toInt() != 0,
setting.attributeNode( "shortTitle" ).value(),
setting.attributeNode( "showInAttributeTable" ).value().toInt() != 0 )
);
}
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsactionmanager.h
Expand Up @@ -138,7 +138,7 @@ class CORE_EXPORT QgsActionManager
/**
* Get the action at the specified index.
*/
QgsAction at( int idx ) const { return mActions.at( idx ); }
const QgsAction& at( int idx ) const { return mActions.at( idx ); }

/**
* Get the action at the specified index.
Expand Down
36 changes: 21 additions & 15 deletions src/gui/attributetable/qgsattributetabledelegate.cpp
Expand Up @@ -17,6 +17,7 @@
#include <QLineEdit>
#include <QComboBox>
#include <QPainter>
#include <QToolButton>

#include "qgsattributeeditor.h"
#include "qgsattributetabledelegate.h"
Expand All @@ -27,6 +28,7 @@
#include "qgsfeatureselectionmodel.h"
#include "qgslogger.h"
#include "qgsvectordataprovider.h"
#include "qgsactionmanager.h"


QgsVectorLayer* QgsAttributeTableDelegate::layer( const QAbstractItemModel *model )
Expand Down Expand Up @@ -55,10 +57,7 @@ const QgsAttributeTableModel* QgsAttributeTableDelegate::masterModel( const QAbs
return nullptr;
}

QWidget *QgsAttributeTableDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const
QWidget* QgsAttributeTableDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
Q_UNUSED( option );
QgsVectorLayer *vl = layer( index.model() );
Expand Down Expand Up @@ -119,9 +118,8 @@ void QgsAttributeTableDelegate::setFeatureSelectionModel( QgsFeatureSelectionMod
mFeatureSelectionModel = featureSelectionModel;
}

void QgsAttributeTableDelegate::paint( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const

void QgsAttributeTableDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();

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

QItemDelegate::paint( painter, myOpt, index );

if ( option.state & QStyle::State_HasFocus )
if ( index.column() == 0 )
{
painter->drawImage( QPoint( 0, 0 ), mActionButtonImage );
}
else
{
QRect r = option.rect.adjusted( 1, 1, -1, -1 );
QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
painter->save();
painter->setPen( p );
painter->drawRect( r );
painter->restore();
QItemDelegate::paint( painter, myOpt, index );

if ( option.state & QStyle::State_HasFocus )
{
QRect r = option.rect.adjusted( 1, 1, -1, -1 );
QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
painter->save();
painter->setPen( p );
painter->drawRect( r );
painter->restore();
}
}
}

0 comments on commit 1827546

Please sign in to comment.