Skip to content

Commit

Permalink
Change many items from scripts faster
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Apr 22, 2024
1 parent 65ec426 commit c13eb69
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 81 deletions.
13 changes: 7 additions & 6 deletions src/gui/clipboardbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,6 @@ bool ClipboardBrowser::isFiltered(int row) const
&& !m_sharedData->itemFactory->matches(ind, *filter);
}

QVariantMap ClipboardBrowser::itemData(const QModelIndex &index) const
{
return index.data(contentType::data).toMap();
}

bool ClipboardBrowser::hideFiltered(int row)
{
const bool hide = isFiltered(row);
Expand Down Expand Up @@ -1710,6 +1705,12 @@ void ClipboardBrowser::addUnique(const QVariantMap &data, ClipboardMode mode)
add(data);
}

void ClipboardBrowser::setItemsData(const QMap<QPersistentModelIndex, QVariantMap> &itemsData)
{
if ( isLoaded() )
m.setItemsData(itemsData);
}

bool ClipboardBrowser::loadItems()
{
if ( isLoaded() )
Expand Down Expand Up @@ -1858,7 +1859,7 @@ QWidget *ClipboardBrowser::currentItemPreview(QWidget *parent)
return nullptr;

const QModelIndex index = currentIndex();
const auto data = itemData(index);
const auto data = index.data(contentType::data).toMap();
return d.createPreview(data, parent);
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/clipboardbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class ClipboardBrowser final : public QListView
*/
void addUnique(const QVariantMap &data, ClipboardMode mode);

void setItemsData(const QMap<QPersistentModelIndex, QVariantMap> &itemsData);

/** Number of items in list. */
int length() const { return m.rowCount(); }

Expand Down Expand Up @@ -186,8 +188,6 @@ class ClipboardBrowser final : public QListView
*/
bool isFiltered(int row) const;

QVariantMap itemData(const QModelIndex &index) const;

bool isLoaded() const;

/**
Expand Down
29 changes: 29 additions & 0 deletions src/item/clipboardmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ void ClipboardModel::insertItems(const QVector<QVariantMap> &dataList, int row)
endInsertRows();
}

void ClipboardModel::setItemsData(const QMap<QPersistentModelIndex, QVariantMap> &itemsData)
{
QPersistentModelIndex topIndex;
QPersistentModelIndex bottomIndex;

for (auto it = std::begin(itemsData); it != std::end(itemsData); ++it) {
const QPersistentModelIndex &index = it.key();
if ( !index.isValid() )
continue;

const int row = index.row();
ClipboardItem &item = m_clipboardList[row];

// Emit dataChanged() only if really changed.
if ( item.setData(it.value()) ) {
if ( !topIndex.isValid() ) {
topIndex = index;
bottomIndex = index;
} else {
topIndex = std::min(topIndex, index);
bottomIndex = std::max(bottomIndex, index);
}
}
}

if ( topIndex.isValid() )
emit dataChanged(topIndex, bottomIndex);
}

bool ClipboardModel::insertRows(int position, int rows, const QModelIndex&)
{
if ( rows <= 0 || position < 0 )
Expand Down
2 changes: 2 additions & 0 deletions src/item/clipboardmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class ClipboardModel final : public QAbstractListModel

void insertItems(const QVector<QVariantMap> &dataList, int row);

void setItemsData(const QMap<QPersistentModelIndex, QVariantMap> &itemsData);

/**
* Sort items in ascending order.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/item/itemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void ItemDelegate::createItemWidget(const QModelIndex &index)
const int row = index.row();
ItemWidget *w = m_items[row].get();
if (w == nullptr) {
auto data = m_view->itemData(index);
QVariantMap data = index.data(contentType::data).toMap();
data.insert(mimeCurrentTab, m_view->tabName());
w = updateWidget(index, data);
emit itemWidgetCreated(PersistentDisplayItem(this, data, w->widget()));
Expand Down
159 changes: 89 additions & 70 deletions src/scriptable/scriptableproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,15 +1341,14 @@ void ScriptableProxy::browserMoveSelected(int targetRow)
{
INVOKE2(browserMoveSelected, (targetRow));

const QList<QPersistentModelIndex> selected = selectedIndexes();
if ( selected.isEmpty() )
return;

ClipboardBrowser *c = m_wnd->browserForItem(selected.first());
QList<QPersistentModelIndex> selected = selectedIndexes();
selectionRemoveInvalid(&selected);
ClipboardBrowser *c = browserForIndexes(selected);
if (c == nullptr)
return;

QModelIndexList indexes;
indexes.reserve(selected.size());
for (const auto &index : selected)
indexes.append(index);
c->move(indexes, targetRow);
Expand Down Expand Up @@ -1535,19 +1534,22 @@ QString ScriptableProxy::browserChange(const QString &tabName, int row, const Va
if (!c)
return QStringLiteral("Invalid tab");

QMap<QPersistentModelIndex, QVariantMap> itemsData;

int currentRow = row;
for (const auto &data : items.items) {
const auto index = c->index(currentRow);
QVariantMap itemData = c->model()->data(index, contentType::data).toMap();
QVariantMap itemData = index.data(contentType::data).toMap();
for (auto it = data.constBegin(); it != data.constEnd(); ++it) {
if ( it.value().isValid() )
itemData.insert( it.key(), it.value() );
else
itemData.remove( it.key() );
}
c->model()->setData(index, itemData, contentType::data);
itemsData[index] = itemData;
++currentRow;
}
c->setItemsData(itemsData);

return QString();
}
Expand Down Expand Up @@ -1613,12 +1615,12 @@ QVector<int> ScriptableProxy::selectedItems()
INVOKE(selectedItems, ());

QVector<int> selectedRows;
const QList<QPersistentModelIndex> selected = selectedIndexes();
QList<QPersistentModelIndex> selected = selectedIndexes();
selectionRemoveInvalid(&selected);
selectedRows.reserve(selected.count());
for (const auto &index : selected) {
if (index.isValid())
selectedRows.append(index.row());
}

for (const auto &index : selected)
selectedRows.append(index.row());

return selectedRows;
}
Expand All @@ -1627,51 +1629,47 @@ QVariantMap ScriptableProxy::selectedItemData(int selectedIndex)
{
INVOKE(selectedItemData, (selectedIndex));

auto c = currentBrowser();
if (!c)
return QVariantMap();

const auto index = selectedIndexes().value(selectedIndex);
Q_ASSERT( !index.isValid() || index.model() == c->model() );
if ( !index.isValid() )
return {};

ClipboardBrowser *c = m_wnd->browserForItem(index);
if (c == nullptr)
return {};

return c->copyIndex(index);
}

bool ScriptableProxy::setSelectedItemData(int selectedIndex, const QVariantMap &data)
{
INVOKE(setSelectedItemData, (selectedIndex, data));

auto c = currentBrowser();
if (!c)
return false;

const auto index = selectedIndexes().value(selectedIndex);
if ( !index.isValid() )
return false;

Q_ASSERT( index.model() == c->model() );
ClipboardBrowser *c = m_wnd->browserForItem(index);
if (c == nullptr)
return false;

return c->model()->setData(index, data, contentType::data);
}

VariantMapList ScriptableProxy::selectedItemsData()
{
INVOKE(selectedItemsData, ());

auto c = currentBrowser();
if (!c)
QList<QPersistentModelIndex> selected = selectedIndexes();
selectionRemoveInvalid(&selected);
ClipboardBrowser *c = browserForIndexes(selected);
if (c == nullptr)
return {};

const auto model = c->model();

QVector<QVariantMap> dataList;
const auto selected = selectedIndexes();
dataList.reserve(selected.size());

for (const auto &index : selected) {
if ( index.isValid() ) {
Q_ASSERT( index.model() == model );
dataList.append( c->copyIndex(index) );
}
}
for (const auto &index : selected)
dataList.append( c->copyIndex(index) );

return {dataList};
}
Expand All @@ -1680,21 +1678,23 @@ void ScriptableProxy::setSelectedItemsData(const VariantMapList &dataList)
{
INVOKE2(setSelectedItemsData, (dataList));

auto c = currentBrowser();
if (!c)
return;
const QList<QPersistentModelIndex> selected = selectedIndexes();
ClipboardBrowser *c = nullptr;
QMap<QPersistentModelIndex, QVariantMap> itemsData;
const auto count = std::min( selected.size(), dataList.items.size() );
for ( int i = 0; i < count; ++i ) {
const auto &index = selected[i];
if ( !index.isValid() )
continue;

const auto model = c->model();
if (c == nullptr)
c = m_wnd->browserForItem(index);

const auto indexes = selectedIndexes();
const auto count = std::min( indexes.size(), dataList.items.size() );
for ( int i = 0; i < count; ++i ) {
const auto &index = indexes[i];
if ( index.isValid() ) {
Q_ASSERT( index.model() == model );
model->setData(index, dataList.items[i], contentType::data);
}
itemsData[index] = dataList.items[i];
}

if (c != nullptr)
c->setItemsData(itemsData);
}

int ScriptableProxy::createSelection(const QString &tabName)
Expand Down Expand Up @@ -1854,12 +1854,12 @@ void ScriptableProxy::selectionGetCurrent(int id)
if (!selection.browser)
return;

auto selected = selectedIndexes();
QList<QPersistentModelIndex> selected = selectedIndexes();
selectionRemoveInvalid(&selected);
if ( selected.isEmpty() ) {
m_selections[id] = {nullptr, {}};
} else {
ClipboardBrowser *c = m_wnd->browserForItem(selected.first());
ClipboardBrowser *c = browserForIndexes(selected);
m_selections[id] = {c, selected};
}
}
Expand Down Expand Up @@ -1930,13 +1930,16 @@ void ScriptableProxy::selectionSetItemsData(int id, const QVariantList &dataList
{
INVOKE2(selectionSetItemsData, (id, dataList));

QMap<QPersistentModelIndex, QVariantMap> itemsData;
const auto selection = m_selections.value(id);
const auto count = std::min( selection.indexes.size(), dataList.size() );
for ( int i = 0; i < count; ++i ) {
const auto &index = selection.indexes[i];
if ( index.isValid() )
selection.browser->model()->setData(index, dataList[i], contentType::data);
itemsData[index] = dataList[i].toMap();
}

selection.browser->setItemsData(itemsData);
}

QVariantList ScriptableProxy::selectionGetItemsFormat(int id, const QString &format)
Expand All @@ -1957,16 +1960,7 @@ void ScriptableProxy::selectionSetItemsFormat(int id, const QString &mime, const
INVOKE2(selectionSetItemsFormat, (id, mime, value));

const auto selection = m_selections.value(id);
for (const auto &index : selection.indexes) {
if ( index.isValid() ) {
QVariantMap data = index.data(contentType::data).toMap();
if (value.isValid())
data[mime] = value;
else
data.remove(mime);
selection.browser->model()->setData(index, data, contentType::data);
}
}
setItemsData(selection.browser, selection.indexes, mime, value);
}

void ScriptableProxy::selectionMove(int id, int row)
Expand Down Expand Up @@ -2187,18 +2181,14 @@ int ScriptableProxy::inputDialog(const NamedValueList &values)
void ScriptableProxy::setSelectedItemsData(const QString &mime, const QVariant &value)
{
INVOKE2(setSelectedItemsData, (mime, value));
const QList<QPersistentModelIndex> selected = selectedIndexes();
for (const auto &index : selected) {
ClipboardBrowser *c = m_wnd->browserForItem(index);
if (c) {
QVariantMap data = c->model()->data(index, contentType::data).toMap();
if (value.isValid())
data[mime] = value;
else
data.remove(mime);
c->model()->setData(index, data, contentType::data);
}
}

QList<QPersistentModelIndex> selected = selectedIndexes();
selectionRemoveInvalid(&selected);
ClipboardBrowser *c = browserForIndexes(selected);
if (c == nullptr)
return;

setItemsData(c, selected, mime, value);
}

void ScriptableProxy::filter(const QString &text)
Expand Down Expand Up @@ -2630,6 +2620,26 @@ QByteArray ScriptableProxy::itemData(const QString &tabName, int i, const QStrin
return data.value(mime).toByteArray();
}

void ScriptableProxy::setItemsData(
ClipboardBrowser *c, const QList<QPersistentModelIndex> &indexes, const QString &mime, const QVariant &value)
{
QMap<QPersistentModelIndex, QVariantMap> itemsData;

for (const auto &index : indexes) {
if ( !index.isValid() )
continue;

QVariantMap data = index.data(contentType::data).toMap();
if (value.isValid())
data[mime] = value;
else
data.remove(mime);
itemsData[index] = data;
}

c->setItemsData(itemsData);
}

ClipboardBrowser *ScriptableProxy::currentBrowser() const
{
const QString currentTabName = m_actionData.value(mimeCurrentTab).toString();
Expand All @@ -2651,6 +2661,15 @@ QList<QPersistentModelIndex> ScriptableProxy::selectedIndexes() const
.value< QList<QPersistentModelIndex> >();
}

ClipboardBrowser *ScriptableProxy::browserForIndexes(const QList<QPersistentModelIndex> &indexes) const
{
for (const auto &index : indexes) {
if ( index.isValid() )
return m_wnd->browserForItem(index);
}
return nullptr;
}

QVariant ScriptableProxy::waitForFunctionCallFinished(int functionCallId)
{
if (m_disconnected)
Expand Down
Loading

0 comments on commit c13eb69

Please sign in to comment.