Skip to content

Commit a2e4476

Browse files
committed
Add editItem() for editing any format
Fixes #2672
1 parent bee0248 commit a2e4476

14 files changed

+116
-78
lines changed

docs/scripting-api.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,13 @@ unlike in GUI, where row numbers start from 1 by default.
494494

495495
.. js:function:: edit([row|text] ...)
496496

497-
Edits items in current tab.
497+
Edits items in the current tab.
498+
499+
Opens external editor if set, otherwise opens internal editor.
500+
501+
.. js:function:: editItem(row, [mimeType, [data]])
502+
503+
Edits specific format for the item.
498504

499505
Opens external editor if set, otherwise opens internal editor.
500506

src/gui/clipboardbrowser.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,18 @@ void ClipboardBrowser::setEditorWidget(ItemEditorWidget *editor, bool changeClip
425425
setHorizontalScrollBarPolicy(scrollbarPolicy);
426426
}
427427

428-
void ClipboardBrowser::editItem(const QModelIndex &index, bool editNotes, bool changeClipboard)
428+
void ClipboardBrowser::editItem(
429+
const QModelIndex &index, const QString &format, bool changeClipboard)
429430
{
430431
if (!index.isValid())
431432
return;
432433

433-
ItemEditorWidget *editor = d.createCustomEditor(this, index, editNotes);
434+
ItemEditorWidget *editor = d.createCustomEditor(this, index, format);
434435
if (editor != nullptr && editor->isValid() ) {
435436
setEditorWidget(editor, changeClipboard);
436437
} else {
437438
delete editor;
438-
if (!editNotes)
439-
openEditor(index);
439+
openEditor(index, format);
440440
}
441441
}
442442

@@ -1213,19 +1213,21 @@ void ClipboardBrowser::doItemsLayout()
12131213
bool ClipboardBrowser::openEditor()
12141214
{
12151215
const QModelIndexList selected = selectionModel()->selectedRows();
1216-
return (selected.size() == 1) ? openEditor( selected.first() )
1217-
: openEditor( selectedText().toUtf8() );
1216+
return (selected.size() == 1) ? openEditor( selected.first(), mimeText )
1217+
: openEditor( {}, mimeText, selectedText().toUtf8() );
12181218
}
12191219

1220-
bool ClipboardBrowser::openEditor(const QByteArray &textData, bool changeClipboard)
1220+
bool ClipboardBrowser::openEditor(
1221+
const QModelIndex &index, const QString &format, const QByteArray &content, bool changeClipboard)
12211222
{
12221223
if ( !isLoaded() )
12231224
return false;
12241225

12251226
if ( m_sharedData->editor.isEmpty() )
12261227
return false;
12271228

1228-
QObject *editor = new ItemEditor(textData, mimeText, m_sharedData->editor, this);
1229+
auto editor = new ItemEditor(content, format, m_sharedData->editor, this);
1230+
editor->setIndex(index);
12291231
if ( !startEditor(editor) )
12301232
return false;
12311233

@@ -1237,7 +1239,7 @@ bool ClipboardBrowser::openEditor(const QByteArray &textData, bool changeClipboa
12371239
return true;
12381240
}
12391241

1240-
bool ClipboardBrowser::openEditor(const QModelIndex &index)
1242+
bool ClipboardBrowser::openEditor(const QModelIndex &index, const QString &format)
12411243
{
12421244
if ( !isLoaded() )
12431245
return false;
@@ -1253,7 +1255,7 @@ bool ClipboardBrowser::openEditor(const QModelIndex &index)
12531255
if ( !m_sharedData->editor.trimmed().isEmpty() ) {
12541256
const QString text = getTextData(data);
12551257
if ( !text.isNull() ) {
1256-
auto itemEditor = new ItemEditor( text.toUtf8(), mimeText, m_sharedData->editor, this );
1258+
auto itemEditor = new ItemEditor( text.toUtf8(), format, m_sharedData->editor, this );
12571259
itemEditor->setIndex(index);
12581260
if ( startEditor(itemEditor) )
12591261
return true;
@@ -1272,7 +1274,7 @@ void ClipboardBrowser::editNotes()
12721274
scrollTo(ind, PositionAtTop);
12731275
emit requestShow(this);
12741276

1275-
editItem(ind, true);
1277+
editItem(ind, mimeItemNotes);
12761278
}
12771279

12781280
void ClipboardBrowser::itemModified(const QByteArray &bytes, const QString &mime, const QModelIndex &index)
@@ -1357,7 +1359,7 @@ void ClipboardBrowser::moveToClipboard(const QModelIndexList &indexes)
13571359
emit changeClipboard(data);
13581360
}
13591361

1360-
void ClipboardBrowser::editNew(const QString &text, bool changeClipboard)
1362+
void ClipboardBrowser::editNew(const QString &format, const QByteArray &content, bool changeClipboard)
13611363
{
13621364
if ( !isLoaded() )
13631365
return;
@@ -1366,11 +1368,11 @@ void ClipboardBrowser::editNew(const QString &text, bool changeClipboard)
13661368
filterItems(nullptr);
13671369

13681370
m_selectNewItems = true;
1369-
const bool added = add(text);
1371+
const bool added = add( createDataMap(format, content) );
13701372
m_selectNewItems = false;
13711373

13721374
if (added)
1373-
editItem(currentIndex(), false, changeClipboard);
1375+
editItem(currentIndex(), format, changeClipboard);
13741376
}
13751377

13761378
void ClipboardBrowser::keyPressEvent(QKeyEvent *event)
@@ -1532,7 +1534,7 @@ void ClipboardBrowser::editSelected()
15321534
QModelIndex ind = currentIndex();
15331535
if ( ind.isValid() ) {
15341536
emit requestShow(this);
1535-
editItem(ind);
1537+
editItem(ind, mimeText);
15361538
}
15371539
}
15381540
}
@@ -1797,9 +1799,9 @@ void ClipboardBrowser::setStoreItems(bool store)
17971799
::removeItems(m_tabName);
17981800
}
17991801

1800-
void ClipboardBrowser::editRow(int row)
1802+
void ClipboardBrowser::editRow(int row, const QString &format)
18011803
{
1802-
editItem( index(row) );
1804+
editItem( index(row), format );
18031805
}
18041806

18051807
void ClipboardBrowser::move(int key)

src/gui/clipboardbrowser.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ class ClipboardBrowser final : public QListView
140140
/** Show only items matching the regular expression. */
141141
void filterItems(const ItemFilterPtr &filter);
142142
/** Open editor. */
143-
bool openEditor(const QByteArray &textData, bool changeClipboard = false);
143+
bool openEditor(const QModelIndex &index, const QString &format, const QByteArray &content, bool changeClipboard = false);
144144
/** Open editor for an item. */
145-
bool openEditor(const QModelIndex &index);
145+
bool openEditor(const QModelIndex &index, const QString &format);
146146

147147
/** Set current item. */
148148
void setCurrent(int row, bool keepSelection = false, bool setCurrentOnly = false);
@@ -156,12 +156,10 @@ class ClipboardBrowser final : public QListView
156156
* Create and edit new item.
157157
*/
158158
void editNew(
159-
const QString &text = QString(), //!< Text of new item.
160-
bool changeClipboard = false //!< Change clipboard if item is modified.
161-
);
159+
const QString &format, const QByteArray &content = {}, bool changeClipboard = false);
162160

163161
/** Edit item in given @a row. */
164-
void editRow(int row);
162+
void editRow(int row, const QString &format);
165163

166164
void move(int key);
167165

@@ -329,7 +327,7 @@ class ClipboardBrowser final : public QListView
329327

330328
void setEditorWidget(ItemEditorWidget *editor, bool changeClipboard = false);
331329

332-
void editItem(const QModelIndex &index, bool editNotes = false, bool changeClipboard = false);
330+
void editItem(const QModelIndex &index, const QString &format, bool changeClipboard = false);
333331

334332
void updateEditorGeometry();
335333

src/gui/commandcompleterdocumentation.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ void addDocumentation(AddDocumentationCallback addDocumentation)
5252
addDocumentation("insert", "insert(row, text|Item...)", "Inserts new items to current tab.");
5353
addDocumentation("remove", "remove(row, ...)", "Removes items in current tab.");
5454
addDocumentation("move", "move(row)", "Moves selected items to given row in same tab.");
55-
addDocumentation("edit", "edit([row|text] ...)", "Edits items in current tab.");
55+
addDocumentation("edit", "edit([row|text] ...)", "Edits items in the current tab.");
56+
addDocumentation("editItem", "editItem(row, [mimeType, [data]])", "Edits specific format for the item.");
5657
addDocumentation("read", "read([mimeType])", "Same as `clipboard()`.");
5758
addDocumentation("read", "read(mimeType, row, ...) -> `ByteArray`", "Returns concatenated data from items, or clipboard if row is negative.");
5859
addDocumentation("write", "write(row, mimeType, data, [mimeType, data]...)", "Inserts new item to current tab.");
@@ -175,8 +176,8 @@ void addDocumentation(AddDocumentationCallback addDocumentation)
175176
addDocumentation("onItemsAdded", "onItemsAdded()", "Called when items are added to a tab.");
176177
addDocumentation("onItemsRemoved", "onItemsRemoved()", "Called when items are being removed from a tab.");
177178
addDocumentation("onItemsChanged", "onItemsChanged()", "Called when data in items change.");
178-
addDocumentation("onTabSelected", "onTabSelected()", "Called when another tab is open.");
179-
addDocumentation("onItemsLoaded", "onItemsLoaded()", "Called when items a loaded to a tab.");
179+
addDocumentation("onTabSelected", "onTabSelected()", "Called when another tab is opened.");
180+
addDocumentation("onItemsLoaded", "onItemsLoaded()", "Called when all items are loaded into a tab.");
180181
addDocumentation("ByteArray", "ByteArray", "Wrapper for QByteArray Qt class.");
181182
addDocumentation("File", "File", "Wrapper for QFile Qt class.");
182183
addDocumentation("Dir", "Dir", "Wrapper for QDir Qt class.");

src/gui/mainwindow.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "gui/icons.h"
3737
#include "gui/logdialog.h"
3838
#include "gui/notification.h"
39-
#include "gui/notificationbutton.h"
4039
#include "gui/notificationdaemon.h"
4140
#include "gui/tabdialog.h"
4241
#include "gui/tabicons.h"
@@ -3813,7 +3812,7 @@ void MainWindow::editNewItem()
38133812
showWindow();
38143813
if ( !c->isInternalEditorOpen() ) {
38153814
c->setFocus();
3816-
c->editNew();
3815+
c->editNew(mimeText);
38173816
}
38183817
}
38193818

src/item/itemdelegate.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -230,33 +230,28 @@ void ItemDelegate::updateItemSize(const QModelIndex &index, QSize itemWidgetSize
230230
}
231231

232232
ItemEditorWidget *ItemDelegate::createCustomEditor(
233-
QWidget *parent, const QModelIndex &index, bool editNotes)
233+
QWidget *parent, const QModelIndex &index, const QString &format)
234234
{
235-
bool hasHtml = false;
236-
QString text;
237-
if (editNotes) {
238-
text = index.data(contentType::notes).toString();
239-
} else {
240-
const QVariantMap data = m_sharedData->itemFactory->data(index);
241-
text = getTextData(data, mimeHtml);
242-
if (text.isEmpty()) {
243-
text = getTextData(data);
244-
if (text.isNull())
245-
return nullptr;
246-
} else {
247-
hasHtml = true;
248-
}
249-
}
235+
// Refuse editing non-text data
236+
if ( format != mimeItemNotes && !format.startsWith(QLatin1String("text/")) )
237+
return nullptr;
238+
239+
const QVariantMap data = m_sharedData->itemFactory->data(index);
240+
if ( format != mimeItemNotes && !data.contains(format) )
241+
return nullptr;
250242

251243
auto editorParent = new QWidget(parent);
252-
auto editor = new ItemEditorWidget(index, editNotes, editorParent);
244+
auto editor = new ItemEditorWidget(index, format, editorParent);
253245

254246
connect(editor, &QObject::destroyed, editorParent, &QObject::deleteLater);
255247

256-
if (hasHtml) {
257-
editor->setHtml(text);
248+
// Prefer editing rich text
249+
if ( format == mimeText && data.contains(mimeHtml) ) {
250+
const QString html = getTextData(data, mimeHtml);
251+
editor->setHtml(html);
258252
sanitizeTextDocument(editor->document());
259253
} else {
254+
const QString text = getTextData(data, format);
260255
editor->setPlainText(text);
261256
}
262257

src/item/itemdelegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class ItemDelegate final : public QItemDelegate
6969
void setItemSizes(int maxWidth, int idealWidth);
7070

7171
/** Create internal item editor widget. */
72-
ItemEditorWidget *createCustomEditor(QWidget *parent, const QModelIndex &index,
73-
bool editNotes);
72+
ItemEditorWidget *createCustomEditor(
73+
QWidget *parent, const QModelIndex &index, const QString &format);
7474

7575
/**
7676
* Highlight matched text with current search expression, font and color.

src/item/itemeditorwidget.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ QAction *addMenuItem(const MenuItem &menuItem, QToolBar *toolBar, ItemEditorWidg
5151

5252
} // namespace
5353

54-
ItemEditorWidget::ItemEditorWidget(const QModelIndex &index, bool editNotes, QWidget *parent)
54+
ItemEditorWidget::ItemEditorWidget(const QModelIndex &index, const QString &format, QWidget *parent)
5555
: QTextEdit(parent)
5656
, m_index(index)
5757
, m_saveOnReturnKey(false)
58-
, m_editNotes(editNotes)
58+
, m_format(format)
5959
{
6060
setFrameShape(QFrame::NoFrame);
6161
setFocusPolicy(Qt::StrongFocus);
@@ -84,12 +84,12 @@ void ItemEditorWidget::setSaveOnReturnKey(bool enabled)
8484
QVariantMap ItemEditorWidget::data() const
8585
{
8686
QVariantMap data;
87-
if (m_editNotes) {
88-
setTextData( &data, toPlainText(), mimeItemNotes );
89-
} else {
87+
if (m_format == mimeText) {
9088
setTextData( &data, toPlainText(), mimeText );
9189
if ( containsRichText(*document()) )
9290
setTextData( &data, toHtml(), mimeHtml );
91+
} else {
92+
setTextData( &data, toPlainText(), m_format );
9393
}
9494
return data;
9595
}

src/item/itemeditorwidget.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ItemEditorWidget final : public QTextEdit
2626
{
2727
Q_OBJECT
2828
public:
29-
ItemEditorWidget(const QModelIndex &index, bool editNotes, QWidget *parent = nullptr);
29+
ItemEditorWidget(const QModelIndex &index, const QString &format, QWidget *parent = nullptr);
3030

3131
bool isValid() const;
3232

@@ -77,7 +77,7 @@ class ItemEditorWidget final : public QTextEdit
7777

7878
QPersistentModelIndex m_index;
7979
bool m_saveOnReturnKey;
80-
bool m_editNotes;
80+
QString m_format;
8181
ItemFilterPtr m_filter;
8282
};
8383

src/scriptable/scriptable.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,36 +1138,66 @@ void Scriptable::edit()
11381138
m_skipArguments = -1;
11391139

11401140
QJSValue value;
1141-
QString text;
1141+
QByteArray content;
11421142
int row = -1;
11431143

11441144
const int len = argumentCount();
11451145
for ( int i = 0; i < len; ++i ) {
11461146
value = argument(i);
11471147
if (i > 0)
1148-
text.append(m_inputSeparator);
1148+
content.append(m_inputSeparator.toUtf8());
11491149
if ( toInt(value, &row) ) {
11501150
const QByteArray bytes = row >= 0 ? m_proxy->browserItemData(m_tabName, row, mimeText)
11511151
: getClipboardData(mimeText);
1152-
text.append( getTextData(bytes) );
1152+
content.append(bytes);
11531153
} else {
1154-
text.append( toString(value) );
1154+
content.append( toByteArray(value) );
11551155
}
11561156
}
11571157

11581158
bool changeClipboard = row < 0;
11591159

1160-
if ( !m_proxy->browserOpenEditor(m_tabName, fromString(text), changeClipboard) ) {
1160+
if ( !m_proxy->browserOpenEditor(m_tabName, row, mimeText, content, changeClipboard) ) {
11611161
m_proxy->showBrowser(m_tabName);
11621162
if (len == 1 && row >= 0) {
11631163
m_proxy->browserSetCurrent(m_tabName, row);
1164-
m_proxy->browserEditRow(m_tabName, row);
1164+
m_proxy->browserEditRow(m_tabName, row, mimeText);
11651165
} else {
1166-
m_proxy->browserEditNew(m_tabName, text, changeClipboard);
1166+
m_proxy->browserEditNew(m_tabName, mimeText, content, changeClipboard);
11671167
}
11681168
}
11691169
}
11701170

1171+
QJSValue Scriptable::editItem()
1172+
{
1173+
m_skipArguments = 3;
1174+
1175+
int row;
1176+
if ( !toInt(argument(0), &row) ) {
1177+
return throwError(argumentError());
1178+
}
1179+
1180+
const auto format = arg(1, mimeText);
1181+
const bool changeClipboard = row < 0;
1182+
const QByteArray content = argumentCount() > 2
1183+
? makeByteArray( argument(2) )
1184+
: ( row >= 0
1185+
? m_proxy->browserItemData(m_tabName, row, format)
1186+
: getClipboardData(format));
1187+
1188+
if ( !m_proxy->browserOpenEditor(m_tabName, row, format, content, changeClipboard) ) {
1189+
m_proxy->showBrowser(m_tabName);
1190+
if (row >= 0) {
1191+
m_proxy->browserSetCurrent(m_tabName, row);
1192+
m_proxy->browserEditRow(m_tabName, row, format);
1193+
} else {
1194+
m_proxy->browserEditNew(m_tabName, format, content, changeClipboard);
1195+
}
1196+
}
1197+
1198+
return {};
1199+
}
1200+
11711201
QJSValue Scriptable::read()
11721202
{
11731203
m_skipArguments = -1;

0 commit comments

Comments
 (0)