Changed the substitution rules for attribute actions to include

access to items that weren't clicked on.

Updated and improved the What's This help for the Actions dialog
box.


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@2201 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
g_j_m 2004-11-03 20:55:42 +00:00
parent 5f02c302b1
commit baa538580e
5 changed files with 79 additions and 20 deletions

View File

@ -39,7 +39,8 @@ void QgsAttributeAction::addAction(QString name, QString action,
mActions.push_back(QgsAction(name, action, capture));
}
void QgsAttributeAction::doAction(unsigned int index, QString value)
void QgsAttributeAction::doAction(unsigned int index, const std::vector<std::pair<QString, QString> >& values,
int defaultValueIndex)
{
aIter action = retrieveAction(index);
@ -55,7 +56,7 @@ void QgsAttributeAction::doAction(unsigned int index, QString value)
if (action != end())
{
QString expanded_action = expandAction(action->action(), value);
QString expanded_action = expandAction(action->action(), values, defaultValueIndex);
std::cout << "Running command '" << expanded_action << "'.\n";
QStringList args = QStringList::split(" ", expanded_action);
@ -89,15 +90,35 @@ QgsAttributeAction::aIter QgsAttributeAction::retrieveAction(unsigned int index)
return a_iter;
}
QString QgsAttributeAction::expandAction(QString action, QString value)
QString QgsAttributeAction::expandAction(QString action, const std::vector<std::pair<QString, QString> >& values,
int clickedOnValue)
{
// This functions currently replaces all % characters in the action
// with the given value. Additional substitutions could include:
// - symbols for $CWD, $HOME, etc (and their OSX and Windows
// equivalents)
// - other values in the same row as the given value
// This function currently replaces all %% characters in the action
// with the value from values[clickedOnValue].second, and then
// searches for all strings that go %attribite_name, where
// attribute_name is found in values[x].first, and replaces any that
// it finds by values[s].second.
QString expanded_action = action.replace("%", value);
// Additional substitutions could include symbols for $CWD, $HOME,
// etc (and their OSX and Windows equivalents)
// This function will potentially fall apart if any of the
// substitutions produce text that could match another
// substition. May be better to adopt a two pass approach - identify
// all matches and their substitutions and then do a second pass
// for the actual substitutions.
QString expanded_action;
if (clickedOnValue >= 0 && clickedOnValue < values.size())
expanded_action = action.replace("%%", values[clickedOnValue].second);
else
expanded_action = action;
for (int i = 0; i < values.size(); ++i)
{
QString to_replace = "%" + values[i].first;
expanded_action = expanded_action.replace(to_replace, values[i].second);
}
return expanded_action;
}

View File

@ -30,7 +30,8 @@
#include <qprocess.h>
#include <list>
#include <vector>
#include <utility>
class QDomNode;
class QDomDocument;
@ -85,8 +86,11 @@ class QgsAttributeAction
// dialog box.
void addAction(QString name, QString action, bool capture = false);
//! Does the action for the given attribute.
void doAction(unsigned int index, QString value);
//! Does the action using the given values. defaultValueIndex is an
// index into values which indicates which value in the values vector
// is to be used if the action has a default placeholder.
void doAction(unsigned int index, const std::vector<std::pair<QString, QString> >& values,
int defaultValueIndex = 0);
//! Returns a const_iterator that points to the QgsAction at the
// given position in the data collection. The insertion order is
@ -111,7 +115,8 @@ class QgsAttributeAction
//! Expands the given action, replacing all %'s with the value as
// given.
static QString expandAction(QString action, QString value);
static QString expandAction(QString action, const std::vector<std::pair<QString, QString> >& values,
int defaultValueIndex);
//! Writes the actions out in XML format
bool writeXML(QDomNode& layer_node, QDomDocument& doc) const;

View File

@ -1,4 +1,4 @@
<!DOCTYPE UI><UI version="3.2" stdsetdef="1">
<!DOCTYPE UI><UI version="3.1" stdsetdef="1">
<class>QgsAttributeActionDialogBase</class>
<widget class="QWidget">
<property name="name">
@ -93,7 +93,11 @@
<string>Enter actions here</string>
</property>
<property name="whatsThis" stdset="0">
<string>Use this table to create actions that can be selected from the Identify Results dialog box. Each action has a name, which appears in the Identify Results popup menu. The action is executed in a shell, and the symbol % is replaced with the value of the parameter that was right-clicked on in the Identify Results dialog box</string>
<string>Use this table to create actions that can be selected from the Identify Results dialog box. Each action has a name, which appears in the Identify Results popup menu. The action is executed as a separate process.
Any %% symbol in the action is replaced by the value of the attribute that was right-clicked on in the Identify Results dialog box.
Any symbols that start with a % and are then followed by the name of an attribute are replaced by the value of that attribute. For example, if there is an attribute called 'filename' with a value of 'text.txt' and there is an action that includes the text '%filename' it will be replaced by text.txt when the action is run.</string>
</property>
</widget>
<widget class="QLayoutWidget">

View File

@ -27,7 +27,7 @@
#include "qgsidentifyresults.h"
QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& aa) : mActions(aa), mActionPopup(0)
QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& aa) : mActions(aa), mClickedOnValue(0), mActionPopup(0)
{
}
@ -77,9 +77,36 @@ void QgsIdentifyResults::popupContextMenu(QListViewItem* item,
mActionPopup->setItemParameter(id, j);
}
}
// Save the attribute value as this is needed for substituting into
// Save the attribute values as these are needed for substituting into
// the action.
mValue = item->text(1);
// A little bit complicated because the user could of right-clicked
// on a parent or a child in the dialog box. We also want to keep
// track of which row in the identify results table was actually
// clicked on. This is stored as an index into the mValues vector.
QListViewItem* parent = item->parent();
QListViewItem* child;
if (item->parent() == 0)
child = item->firstChild();
else
child = parent->firstChild();
mValues.clear();
int j = 0;
while (child != 0)
{
mValues.push_back(std::make_pair(child->text(0), child->text(1)));
// Need to do the comparison on the text strings rather than the
// pointers because if the user clicked on the parent, we need
// to pick up which child that actually is (the parent in the
// identify results dialog box is just one of the children
// that has been chosen by some method).
if (child->text(0) == item->text(0))
mClickedOnValue = j;
++j;
child = child->nextSibling();
}
mActionPopup->popup(p);
}
@ -129,5 +156,5 @@ void QgsIdentifyResults::setTitle(QString title)
// Run the action that was selected in the popup menu
void QgsIdentifyResults::popupItemSelected(int id)
{
mActions.doAction(id, mValue);
mActions.doAction(id, mValues, mClickedOnValue);
}

View File

@ -26,6 +26,7 @@
#include "qgsattributeaction.h"
#include <vector>
#include <map>
class QPopupMenu;
@ -63,8 +64,9 @@ class QgsIdentifyResults:public QgsIdentifyResultsBase
private:
QgsAttributeAction mActions;
int mClickedOnValue;
QPopupMenu* mActionPopup;
QString mValue;
std::vector<std::pair<QString, QString> > mValues;
};
#endif