Lines 25-30
Link Here
|
25 |
#include "urlgrabber.h" |
25 |
#include "urlgrabber.h" |
26 |
|
26 |
|
27 |
#include "ui_editactiondialog.h" |
27 |
#include "ui_editactiondialog.h" |
|
|
28 |
#include <QItemDelegate> |
29 |
#include <QComboBox> |
30 |
|
31 |
namespace { |
32 |
static QString output2text(ClipCommand::Output output) { |
33 |
switch(output) { |
34 |
case ClipCommand::IGNORE: |
35 |
return QString(i18n("Ignore")); |
36 |
case ClipCommand::REPLACE: |
37 |
return QString(i18n("Replace")); |
38 |
case ClipCommand::ADD: |
39 |
return QString(i18n("Add")); |
40 |
} |
41 |
return QString(); |
42 |
} |
43 |
|
44 |
} |
45 |
|
46 |
/** |
47 |
* Show dropdown of editing Output part of commands |
48 |
*/ |
49 |
class ActionOutputDelegate : public QItemDelegate { |
50 |
public: |
51 |
ActionOutputDelegate(QObject* parent = 0) : QItemDelegate(parent){ |
52 |
} |
53 |
|
54 |
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const { |
55 |
QComboBox* editor = new QComboBox(parent); |
56 |
editor->setInsertPolicy(QComboBox::NoInsert); |
57 |
editor->addItem(output2text(ClipCommand::IGNORE), QVariant::fromValue<ClipCommand::Output>(ClipCommand::IGNORE)); |
58 |
editor->addItem(output2text(ClipCommand::REPLACE), QVariant::fromValue<ClipCommand::Output>(ClipCommand::REPLACE)); |
59 |
editor->addItem(output2text(ClipCommand::ADD), QVariant::fromValue<ClipCommand::Output>(ClipCommand::ADD)); |
60 |
return editor; |
61 |
|
62 |
} |
63 |
|
64 |
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const { |
65 |
QComboBox* ed = static_cast<QComboBox*>(editor); |
66 |
QVariant data(index.model()->data(index, Qt::EditRole)); |
67 |
ed->setCurrentIndex(static_cast<int>(data.value<ClipCommand::Output>())); |
68 |
} |
69 |
|
70 |
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { |
71 |
QComboBox* ed = static_cast<QComboBox*>(editor); |
72 |
model->setData(index, ed->itemData(ed->currentIndex())); |
73 |
} |
74 |
|
75 |
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const { |
76 |
editor->setGeometry(option.rect); |
77 |
} |
78 |
}; |
79 |
|
80 |
class ActionDetailModel : public QAbstractTableModel { |
81 |
public: |
82 |
ActionDetailModel(ClipAction* action, QObject* parent = 0); |
83 |
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; |
84 |
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); |
85 |
virtual Qt::ItemFlags flags(const QModelIndex& index) const; |
86 |
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; |
87 |
virtual int columnCount(const QModelIndex& parent) const; |
88 |
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; |
89 |
const QList<ClipCommand>& commands() const { return m_commands; } |
90 |
void addCommand(const ClipCommand& command); |
91 |
void removeCommand(const QModelIndex& index); |
92 |
|
93 |
private: |
94 |
enum column_t { |
95 |
COMMAND_COL = 0, |
96 |
OUTPUT_COL = 1, |
97 |
DESCRIPTION_COL = 2 |
98 |
}; |
99 |
QList<ClipCommand> m_commands; |
100 |
QVariant displayData(ClipCommand* command, column_t colunm) const; |
101 |
QVariant editData(ClipCommand* command, column_t column) const; |
102 |
QVariant decorationData(ClipCommand* command, column_t column) const; |
103 |
void setIconForCommand(ClipCommand& cmd); |
104 |
}; |
105 |
|
106 |
ActionDetailModel::ActionDetailModel(ClipAction* action, QObject* parent): |
107 |
QAbstractTableModel(parent), |
108 |
m_commands(action->commands()) |
109 |
{ |
110 |
|
111 |
} |
112 |
|
113 |
Qt::ItemFlags ActionDetailModel::flags(const QModelIndex& /*index*/) const |
114 |
{ |
115 |
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
116 |
} |
117 |
|
118 |
|
119 |
void ActionDetailModel::setIconForCommand(ClipCommand& cmd) |
120 |
{ |
121 |
// let's try to update icon of the item according to command |
122 |
QString command = cmd.command; |
123 |
if ( command.contains( ' ' ) ) { |
124 |
// get first word |
125 |
command = command.section( ' ', 0, 0 ); |
126 |
} |
127 |
|
128 |
QPixmap iconPix = KIconLoader::global()->loadIcon( |
129 |
command, KIconLoader::Small, 0, |
130 |
KIconLoader::DefaultState, |
131 |
QStringList(), 0, true /* canReturnNull */ ); |
132 |
|
133 |
if ( !iconPix.isNull() ) { |
134 |
cmd.pixmap = command; |
135 |
} else { |
136 |
cmd.pixmap.clear(); |
137 |
} |
138 |
|
139 |
} |
140 |
|
141 |
bool ActionDetailModel::setData(const QModelIndex& index, const QVariant& value, int role) |
142 |
{ |
143 |
if (role == Qt::EditRole) { |
144 |
ClipCommand cmd = m_commands.at(index.row()); |
145 |
switch (static_cast<column_t>(index.column())) { |
146 |
case COMMAND_COL: |
147 |
cmd.command = value.value<QString>(); |
148 |
setIconForCommand(cmd); |
149 |
break; |
150 |
case OUTPUT_COL: |
151 |
cmd.output = value.value<ClipCommand::Output>(); |
152 |
break; |
153 |
case DESCRIPTION_COL: |
154 |
cmd.description = value.value<QString>(); |
155 |
break; |
156 |
} |
157 |
m_commands.replace(index.row(), cmd); |
158 |
emit dataChanged(index, index); |
159 |
return true; |
160 |
} |
161 |
return false; |
162 |
} |
163 |
|
164 |
int ActionDetailModel::columnCount(const QModelIndex& /*parent*/) const |
165 |
{ |
166 |
return 3; |
167 |
} |
168 |
|
169 |
int ActionDetailModel::rowCount(const QModelIndex&) const |
170 |
{ |
171 |
return m_commands.count(); |
172 |
} |
173 |
|
174 |
QVariant ActionDetailModel::displayData(ClipCommand* command, ActionDetailModel::column_t column) const |
175 |
{ |
176 |
switch (column) { |
177 |
case COMMAND_COL: |
178 |
return command->command; |
179 |
case OUTPUT_COL: |
180 |
return output2text(command->output); |
181 |
case DESCRIPTION_COL: |
182 |
return command->description; |
183 |
} |
184 |
return QVariant(); |
185 |
} |
186 |
|
187 |
QVariant ActionDetailModel::decorationData(ClipCommand* command, ActionDetailModel::column_t column) const |
188 |
{ |
189 |
switch (column) { |
190 |
case COMMAND_COL: |
191 |
return command->pixmap.isEmpty() ? KIcon( "system-run" ) : KIcon( command->pixmap ); |
192 |
case OUTPUT_COL: |
193 |
case DESCRIPTION_COL: |
194 |
break; |
195 |
} |
196 |
return QVariant(); |
197 |
|
198 |
} |
199 |
|
200 |
QVariant ActionDetailModel::editData(ClipCommand* command, ActionDetailModel::column_t column) const |
201 |
{ |
202 |
switch (column) { |
203 |
case COMMAND_COL: |
204 |
return command->command; |
205 |
case OUTPUT_COL: |
206 |
return QVariant::fromValue<ClipCommand::Output>(command->output); |
207 |
case DESCRIPTION_COL: |
208 |
return command->description; |
209 |
} |
210 |
return QVariant(); |
211 |
|
212 |
} |
213 |
|
214 |
QVariant ActionDetailModel::headerData(int section, Qt::Orientation orientation, int role) const |
215 |
{ |
216 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
217 |
switch(static_cast<column_t>(section)) { |
218 |
case COMMAND_COL: |
219 |
return i18n("Command"); |
220 |
case OUTPUT_COL: |
221 |
return i18n("Use Output"); |
222 |
case DESCRIPTION_COL: |
223 |
return i18n("Description"); |
224 |
} |
225 |
} |
226 |
return QAbstractTableModel::headerData(section, orientation, role); |
227 |
} |
228 |
|
229 |
|
230 |
QVariant ActionDetailModel::data(const QModelIndex& index, int role) const |
231 |
{ |
232 |
const int column = index.column(); |
233 |
const int row = index.row(); |
234 |
ClipCommand cmd = m_commands.at(row); |
235 |
switch (role) { |
236 |
case Qt::DisplayRole: |
237 |
return displayData(&cmd, static_cast<column_t>(column)); |
238 |
case Qt::DecorationRole: |
239 |
return decorationData(&cmd, static_cast<column_t>(column)); |
240 |
case Qt::EditRole: |
241 |
return editData(&cmd, static_cast<column_t>(column)); |
242 |
} |
243 |
return QVariant(); |
244 |
} |
245 |
|
246 |
void ActionDetailModel::addCommand(const ClipCommand& command) { |
247 |
beginInsertRows(QModelIndex(), rowCount(), rowCount()); |
248 |
m_commands << command; |
249 |
endInsertRows(); |
250 |
} |
251 |
|
252 |
void ActionDetailModel::removeCommand(const QModelIndex& index) { |
253 |
int row = index.row(); |
254 |
beginRemoveRows(QModelIndex(), row, row); |
255 |
m_commands.removeAt(row); |
256 |
endRemoveRows(); |
257 |
|
258 |
} |
28 |
|
259 |
|
29 |
EditActionDialog::EditActionDialog(QWidget* parent) |
260 |
EditActionDialog::EditActionDialog(QWidget* parent) |
30 |
: KDialog(parent) |
261 |
: KDialog(parent) |
Lines 39-57
Link Here
|
39 |
m_ui->pbAddCommand->setIcon(KIcon("list-add")); |
270 |
m_ui->pbAddCommand->setIcon(KIcon("list-add")); |
40 |
m_ui->pbRemoveCommand->setIcon(KIcon("list-remove")); |
271 |
m_ui->pbRemoveCommand->setIcon(KIcon("list-remove")); |
41 |
|
272 |
|
42 |
m_ui->twCommandList->header()->resizeSection( 0, 170 ); |
273 |
// For some reason, the default row height is 30 pixel. Set it to the minimum sectionSize instead, |
43 |
|
274 |
// which is the font height+struts. |
|
|
275 |
m_ui->twCommandList->verticalHeader()->setDefaultSectionSize(m_ui->twCommandList->verticalHeader()->minimumSectionSize()); |
44 |
setMainWidget(dlgWidget); |
276 |
setMainWidget(dlgWidget); |
45 |
|
277 |
|
46 |
connect(m_ui->twCommandList, SIGNAL(itemSelectionChanged()), SLOT(onSelectionChanged())); |
|
|
47 |
connect(m_ui->twCommandList, SIGNAL(itemChanged(QTreeWidgetItem*, int)), |
48 |
SLOT(onItemChanged(QTreeWidgetItem*, int))); |
49 |
|
50 |
connect(m_ui->pbAddCommand, SIGNAL( clicked() ), SLOT( onAddCommand() ) ); |
278 |
connect(m_ui->pbAddCommand, SIGNAL( clicked() ), SLOT( onAddCommand() ) ); |
51 |
connect(m_ui->pbRemoveCommand, SIGNAL( clicked() ), SLOT( onRemoveCommand() ) ); |
279 |
connect(m_ui->pbRemoveCommand, SIGNAL( clicked() ), SLOT( onRemoveCommand() ) ); |
52 |
|
280 |
|
53 |
// update Remove button |
|
|
54 |
onSelectionChanged(); |
55 |
} |
281 |
} |
56 |
|
282 |
|
57 |
EditActionDialog::~EditActionDialog() |
283 |
EditActionDialog::~EditActionDialog() |
Lines 62-67
Link Here
|
62 |
void EditActionDialog::setAction(ClipAction* act, int commandIdxToSelect) |
288 |
void EditActionDialog::setAction(ClipAction* act, int commandIdxToSelect) |
63 |
{ |
289 |
{ |
64 |
m_action = act; |
290 |
m_action = act; |
|
|
291 |
m_model = new ActionDetailModel(act, this); |
292 |
m_ui->twCommandList->setModel(m_model); |
293 |
m_ui->twCommandList->setItemDelegateForColumn(1, new ActionOutputDelegate); |
294 |
connect(m_ui->twCommandList->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(onSelectionChanged())); |
65 |
|
295 |
|
66 |
updateWidgets( commandIdxToSelect ); |
296 |
updateWidgets( commandIdxToSelect ); |
67 |
} |
297 |
} |
Lines 73-97
Link Here
|
73 |
return; |
303 |
return; |
74 |
} |
304 |
} |
75 |
|
305 |
|
76 |
m_ui->twCommandList->clear(); |
|
|
77 |
|
78 |
m_ui->leRegExp->setText(m_action->regExp()); |
306 |
m_ui->leRegExp->setText(m_action->regExp()); |
|
|
307 |
m_ui->automatic->setChecked(m_action->automatic()); |
79 |
m_ui->leDescription->setText(m_action->description()); |
308 |
m_ui->leDescription->setText(m_action->description()); |
80 |
|
309 |
|
81 |
foreach( const ClipCommand& cmd, m_action->commands() ) { |
|
|
82 |
QTreeWidgetItem* item = new QTreeWidgetItem; |
83 |
item->setFlags( item->flags() | Qt::ItemIsEditable ); |
84 |
|
85 |
item->setText( 0, cmd.command ); |
86 |
QString iconName = cmd.pixmap.isEmpty() ? "system-run" : cmd.pixmap; |
87 |
item->setIcon( 0, KIcon( iconName ) ); |
88 |
item->setData( 0, Qt::UserRole, iconName ); // store icon name too |
89 |
item->setText( 1, cmd.description ); |
90 |
m_ui->twCommandList->addTopLevelItem( item ); |
91 |
} |
92 |
|
93 |
if (commandIdxToSelect != -1) { |
310 |
if (commandIdxToSelect != -1) { |
94 |
m_ui->twCommandList->setCurrentItem( m_ui->twCommandList->topLevelItem( commandIdxToSelect ) ); |
311 |
m_ui->twCommandList->setCurrentIndex( m_model->index( commandIdxToSelect ,0 ) ); |
95 |
} |
312 |
} |
96 |
|
313 |
|
97 |
// update Remove button |
314 |
// update Remove button |
Lines 107-122
Link Here
|
107 |
|
324 |
|
108 |
m_action->setRegExp( m_ui->leRegExp->text() ); |
325 |
m_action->setRegExp( m_ui->leRegExp->text() ); |
109 |
m_action->setDescription( m_ui->leDescription->text() ); |
326 |
m_action->setDescription( m_ui->leDescription->text() ); |
|
|
327 |
m_action->setAutomatic( m_ui->automatic->isChecked() ); |
110 |
|
328 |
|
111 |
m_action->clearCommands(); |
329 |
m_action->clearCommands(); |
112 |
|
330 |
|
113 |
int cmdCount = m_ui->twCommandList->topLevelItemCount(); |
331 |
foreach ( const ClipCommand& cmd, m_model->commands() ){ |
114 |
for ( int i=0; i<cmdCount; ++i ) { |
332 |
m_action->addCommand( cmd ); |
115 |
QTreeWidgetItem* item = m_ui->twCommandList->topLevelItem( i ); |
|
|
116 |
// we store icon name in Qt::UserRole in first column |
117 |
// (see onItemChanged()) |
118 |
QString iconName = item->data( 0, Qt::UserRole ).toString(); |
119 |
m_action->addCommand( item->text( 0 ), item->text( 1 ), true, iconName ); |
120 |
} |
333 |
} |
121 |
} |
334 |
} |
122 |
|
335 |
|
Lines 131-184
Link Here
|
131 |
|
344 |
|
132 |
void EditActionDialog::onAddCommand() |
345 |
void EditActionDialog::onAddCommand() |
133 |
{ |
346 |
{ |
134 |
QTreeWidgetItem *item = new QTreeWidgetItem; |
347 |
m_model->addCommand(ClipCommand(i18n( "new command" ), |
135 |
item->setFlags( item->flags() | Qt::ItemIsEditable ); |
348 |
i18n( "Command Description" ), |
136 |
item->setText( 0, i18n( "new command" ) ); |
349 |
true, |
137 |
item->setIcon( 0, KIcon( "system-run" ) ); |
350 |
"" )); |
138 |
item->setText( 1, i18n( "Command Description" ) ); |
351 |
m_ui->twCommandList->edit( m_model->index( m_model->rowCount()-1, 0 )); |
139 |
|
|
|
140 |
m_ui->twCommandList->addTopLevelItem( item ); |
141 |
m_ui->twCommandList->editItem( item ); |
142 |
} |
352 |
} |
143 |
|
353 |
|
144 |
void EditActionDialog::onRemoveCommand() |
354 |
void EditActionDialog::onRemoveCommand() |
145 |
{ |
355 |
{ |
146 |
QTreeWidgetItem* curItem = m_ui->twCommandList->currentItem(); |
356 |
m_model->removeCommand(m_ui->twCommandList->selectionModel()->currentIndex()); |
147 |
delete curItem; |
|
|
148 |
} |
149 |
|
150 |
void EditActionDialog::onItemChanged( QTreeWidgetItem* item, int column ) |
151 |
{ |
152 |
if ( column == 0 ) { |
153 |
// let's try to update icon of the item according to command |
154 |
QString command = item->text( 0 ); |
155 |
if ( command.contains( ' ' ) ) |
156 |
// get first word |
157 |
command = command.section( ' ', 0, 0 ); |
158 |
|
159 |
QPixmap iconPix = KIconLoader::global()->loadIcon( |
160 |
command, KIconLoader::Small, 0, |
161 |
KIconLoader::DefaultState, |
162 |
QStringList(), 0, true /* canReturnNull */ ); |
163 |
|
164 |
// block signals to prevent infinite recursion when setIcon will trigger itemChanged again |
165 |
m_ui->twCommandList->blockSignals( true ); |
166 |
|
167 |
if ( !iconPix.isNull() ) { |
168 |
item->setIcon( 0, KIcon( command ) ); |
169 |
// let's save icon name in data field (if we found something that is not "system-run") |
170 |
item->setData( 0, Qt::UserRole, command ); // command is actually the icon name here :) |
171 |
} else { |
172 |
item->setIcon( 0, KIcon( "system-run" ) ); |
173 |
} |
174 |
|
175 |
m_ui->twCommandList->blockSignals( false ); |
176 |
} |
177 |
} |
357 |
} |
178 |
|
358 |
|
179 |
void EditActionDialog::onSelectionChanged() |
359 |
void EditActionDialog::onSelectionChanged() |
180 |
{ |
360 |
{ |
181 |
m_ui->pbRemoveCommand->setEnabled( !m_ui->twCommandList->selectedItems().isEmpty() ); |
361 |
m_ui->pbRemoveCommand->setEnabled( m_ui->twCommandList->selectionModel() && m_ui->twCommandList->selectionModel()->hasSelection() ); |
182 |
} |
362 |
} |
183 |
|
363 |
|
184 |
#include "editactiondialog.moc" |
364 |
#include "editactiondialog.moc" |