Skip to content

Commit

Permalink
Add forceUnload() script function
Browse files Browse the repository at this point in the history
Fixes updating filtered items and menus after refresh button is clicked.

Signed-off-by: Lukas Holecek <hluk@email.cz>
  • Loading branch information
hluk committed May 30, 2019
1 parent a65b9fb commit 20ec52e
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 51 deletions.
12 changes: 12 additions & 0 deletions docs/scripting-api.rst
Expand Up @@ -278,6 +278,18 @@ omitted.

Returns list of successfully unloaded tabs.

.. js:function:: forceUnload([tabNames...])

Force-unload tabs (i.e. items from memory).

If no tabs are specified, unloads all tabs.

Refresh button needs to be clicked to show the content of a force-unloaded
tab.

If a tab has an editor open, the editor will be closed first even if it has
unsaved changes.

.. js:function:: count(), length(), size()

Returns amount of items in current tab.
Expand Down
8 changes: 7 additions & 1 deletion src/gui/clipboardbrowserplaceholder.cpp
Expand Up @@ -54,7 +54,6 @@ ClipboardBrowser *ClipboardBrowserPlaceholder::createBrowser()
return nullptr;

std::unique_ptr<ClipboardBrowser> c( new ClipboardBrowser(m_tabName, m_sharedData, this) );
emit browserCreated(c.get());

if ( !c->loadItems() ) {
createLoadButton();
Expand All @@ -73,6 +72,7 @@ ClipboardBrowser *ClipboardBrowserPlaceholder::createBrowser()

restartExpiring();

emit browserCreated(m_browser);
return m_browser;
}

Expand Down Expand Up @@ -162,6 +162,8 @@ void ClipboardBrowserPlaceholder::setActiveWidget(QWidget *widget)
layout()->addWidget(widget);
setFocusProxy(widget);
widget->show();
if (isVisible())
widget->setFocus();
}

void ClipboardBrowserPlaceholder::createLoadButton()
Expand All @@ -170,6 +172,8 @@ void ClipboardBrowserPlaceholder::createLoadButton()
return;

m_loadButton = new QPushButton(this);
m_loadButton->setObjectName("ClipboardBrowserRefreshButton");
m_loadButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_loadButton->setFlat(true);

const QIcon icon( getIcon("", IconRedo) );
Expand All @@ -190,6 +194,8 @@ void ClipboardBrowserPlaceholder::unloadBrowser()
m_browser->saveUnsavedItems();
m_browser->deleteLater();
m_browser = nullptr;

emit browserDestroyed();
}

bool ClipboardBrowserPlaceholder::canExpire() const
Expand Down
9 changes: 5 additions & 4 deletions src/gui/clipboardbrowserplaceholder.h
Expand Up @@ -65,8 +65,13 @@ class ClipboardBrowserPlaceholder final : public QWidget
/// Unload browser and data.
bool expire();

void unloadBrowser();

void createLoadButton();

signals:
void browserCreated(ClipboardBrowser *browser);
void browserDestroyed();

protected:
void showEvent(QShowEvent *event) override;
Expand All @@ -75,10 +80,6 @@ class ClipboardBrowserPlaceholder final : public QWidget
private:
void setActiveWidget(QWidget *widget);

void createLoadButton();

void unloadBrowser();

bool canExpire() const;

void restartExpiring();
Expand Down
1 change: 1 addition & 0 deletions src/gui/commandcompleterdocumentation.h
Expand Up @@ -40,6 +40,7 @@ void addDocumentation(AddDocumentationCallback addDocumentation)
addDocumentation("tabIcon", "String tabIcon(tabName)", "Returns path to icon for tab.");
addDocumentation("tabIcon", "tabIcon(tabName, iconPath)", "Sets icon for tab.");
addDocumentation("unload", "String[] unload([tabNames...])", "Unload tabs (i.e. items from memory).");
addDocumentation("forceUnload", "forceUnload([tabNames...])", "Force-unload tabs (i.e. items from memory).");
addDocumentation("count", "count(), length(), size()", "Returns amount of items in current tab.");
addDocumentation("select", "select(row)", "Copies item in the row to clipboard.");
addDocumentation("next", "next()", "Copies next item from current tab to clipboard.");
Expand Down
108 changes: 76 additions & 32 deletions src/gui/mainwindow.cpp
Expand Up @@ -1007,6 +1007,25 @@ void MainWindow::onBrowserCreated(ClipboardBrowser *browser)
ui->searchBar, &Utils::FilterLineEdit::hide );
connect( browser, &ClipboardBrowser::itemWidgetCreated,
this, &MainWindow::onItemWidgetCreated );

const ClipboardBrowserPlaceholder *currentPlaceholder = getPlaceholder();
if (currentPlaceholder && currentPlaceholder->browser() == browser) {
const int index = ui->tabWidget->currentIndex();
tabChanged(index, index);
}

const ClipboardBrowserPlaceholder *placeholderForTrayMenu = getPlaceholderForTrayMenu();
if (placeholderForTrayMenu && placeholderForTrayMenu->browser() == browser)
updateTrayMenu();
}

void MainWindow::onBrowserDestroyed(ClipboardBrowserPlaceholder *placeholder)
{
if (placeholder == getPlaceholder())
updateContextMenu(0);

if (placeholder == getPlaceholderForTrayMenu())
updateTrayMenu();
}

void MainWindow::onItemSelectionChanged(const ClipboardBrowser *browser)
Expand All @@ -1019,7 +1038,9 @@ void MainWindow::onItemsChanged(const ClipboardBrowser *browser)
{
if (browser == this->browser())
updateContextMenu(contextMenuUpdateIntervalMsec);
if (browser == getTabForTrayMenu())

const ClipboardBrowserPlaceholder *placeholder = getPlaceholderForTrayMenu();
if (placeholder && placeholder->browser() == browser)
updateTrayMenu();
}

Expand Down Expand Up @@ -1224,7 +1245,8 @@ bool MainWindow::closeMinimizes() const
ClipboardBrowserPlaceholder *MainWindow::createTab(
const QString &name, TabNameMatching nameMatch)
{
Q_ASSERT( !name.isEmpty() );
if ( name.isEmpty() )
return nullptr;

const int i = nameMatch == MatchExactTabName
? findTabIndexExactMatch(name)
Expand All @@ -1236,6 +1258,8 @@ ClipboardBrowserPlaceholder *MainWindow::createTab(
auto placeholder = new ClipboardBrowserPlaceholder(name, m_sharedData, this);
connect( placeholder, &ClipboardBrowserPlaceholder::browserCreated,
this, &MainWindow::onBrowserCreated );
connect( placeholder, &ClipboardBrowserPlaceholder::browserDestroyed,
this, [this, placeholder]() { onBrowserDestroyed(placeholder); } );

ui->tabWidget->addTab(placeholder, name);
saveTabPositions();
Expand Down Expand Up @@ -1322,12 +1346,13 @@ void MainWindow::addCommandsToTrayMenu(const QVariantMap &clipboardData)
if ( m_menuCommands.isEmpty() )
return;

auto c = getTabForTrayMenu();
if (!c) {
c = getPlaceholder()->createBrowser();
if (!c)
return;
}
ClipboardBrowserPlaceholder *placeholder = getPlaceholderForTrayMenu();
if (!placeholder)
return;

ClipboardBrowser *c = placeholder->createBrowser();
if (!c)
return;

// Pass current window title to commands in tray menu.
auto data = clipboardData;
Expand Down Expand Up @@ -1525,11 +1550,15 @@ QAction *MainWindow::actionForMenuItem(int id, QWidget *parent, Qt::ShortcutCont
return action;
}

void MainWindow::addMenuItems(TrayMenu *menu, ClipboardBrowser *c, int maxItemCount, const QString &searchText)
void MainWindow::addMenuItems(TrayMenu *menu, ClipboardBrowserPlaceholder *placeholder, int maxItemCount, const QString &searchText)
{
WidgetSizeGuard sizeGuard(menu);
menu->clearClipboardItems();

if (!placeholder)
return;

const ClipboardBrowser *c = placeholder->createBrowser();
if (!c)
return;

Expand All @@ -1547,11 +1576,15 @@ void MainWindow::addMenuItems(TrayMenu *menu, ClipboardBrowser *c, int maxItemCo
}
}

void MainWindow::activateMenuItem(ClipboardBrowser *c, const QVariantMap &data, bool omitPaste)
void MainWindow::activateMenuItem(ClipboardBrowserPlaceholder *placeholder, const QVariantMap &data, bool omitPaste)
{
if ( m_sharedData->moveItemOnReturnKey ) {
const auto itemHash = ::hash(data);
c->moveToTop(itemHash);
if (placeholder) {
ClipboardBrowser *c = placeholder->createBrowser();
if (c)
c->moveToTop(itemHash);
}
}

if ( QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier) )
Expand Down Expand Up @@ -2346,16 +2379,11 @@ bool MainWindow::toggleVisible()
return true;
}

void MainWindow::setCurrentTab(const ClipboardBrowser *browser)
void MainWindow::showBrowser(const ClipboardBrowser *browser)
{
int i = 0;
for( ; i < ui->tabWidget->count() && getPlaceholder(i)->browser() != browser; ++i ) {}
setCurrentTab(i);
}

void MainWindow::showBrowser(const ClipboardBrowser *browser)
{
setCurrentTab(browser);
showWindow();
}

Expand All @@ -2370,12 +2398,12 @@ bool MainWindow::setCurrentTab(int index)

void MainWindow::onMenuActionTriggered(const QVariantMap &data, bool omitPaste)
{
activateMenuItem( getTabForMenu(), data, omitPaste );
activateMenuItem( getPlaceholderForMenu(), data, omitPaste );
}

void MainWindow::onTrayActionTriggered(const QVariantMap &data, bool omitPaste)
{
activateMenuItem( getTabForTrayMenu(), data, omitPaste );
activateMenuItem( getPlaceholderForTrayMenu(), data, omitPaste );
}

void MainWindow::trayActivated(QSystemTrayIcon::ActivationReason reason)
Expand Down Expand Up @@ -2816,22 +2844,22 @@ QStringList MainWindow::tabs() const
return ui->tabWidget->tabs();
}

ClipboardBrowser *MainWindow::getTabForMenu()
ClipboardBrowserPlaceholder *MainWindow::getPlaceholderForMenu()
{
const auto i = findTabIndex(m_menuTabName);
return i != -1 ? browser(i) : nullptr;
return i != -1 ? getPlaceholder(i) : nullptr;
}

ClipboardBrowser *MainWindow::getTabForTrayMenu()
ClipboardBrowserPlaceholder *MainWindow::getPlaceholderForTrayMenu()
{
if (m_options.trayCurrentTab)
return browser();
return getPlaceholder();

if ( m_options.trayTabName.isEmpty() )
return m_options.clipboardTab.isEmpty() ? nullptr : tab(m_options.clipboardTab);
return m_options.clipboardTab.isEmpty() ? nullptr : getPlaceholder(m_options.clipboardTab);

int i = findTabIndex(m_options.trayTabName);
return i != -1 ? browser(i) : nullptr;
return i != -1 ? getPlaceholder(i) : nullptr;
}

void MainWindow::onFilterChanged(const QRegExp &re)
Expand Down Expand Up @@ -2987,12 +3015,12 @@ void MainWindow::updateTrayMenuTimeout()

void MainWindow::filterMenuItems(const QString &searchText)
{
addMenuItems(m_menu, getTabForMenu(), m_menuMaxItemCount, searchText);
addMenuItems(m_menu, getPlaceholderForMenu(), m_menuMaxItemCount, searchText);
}

void MainWindow::filterTrayMenuItems(const QString &searchText)
{
addMenuItems(m_trayMenu, getTabForTrayMenu(), m_options.trayItems, searchText);
addMenuItems(m_trayMenu, getPlaceholderForTrayMenu(), m_options.trayItems, searchText);
m_trayMenu->markItemInClipboard(m_clipboardData);
}

Expand Down Expand Up @@ -3132,11 +3160,16 @@ ClipboardBrowser *MainWindow::browserForItem(const QModelIndex &index)
return nullptr;
}

void MainWindow::addTab(const QString &name)
void MainWindow::addAndFocusTab(const QString &name)
{
createTab(name, MatchExactTabName);
auto w = ui->tabWidget;
w->setCurrentIndex( w->count()-1 );
auto placeholder = createTab(name, MatchExactTabName);
if (!placeholder)
return;

int i = 0;
for( ; i < ui->tabWidget->count() && getPlaceholder(i) != placeholder; ++i ) {}
setCurrentTab(i);

saveTabPositions();
}

Expand Down Expand Up @@ -3392,7 +3425,7 @@ void MainWindow::openNewTabDialog(const QString &name)
d->setTabName(name);

connect( d, &TabDialog::newTabNameAccepted,
this, &MainWindow::addTab );
this, &MainWindow::addAndFocusTab );

d->open();
}
Expand Down Expand Up @@ -3564,6 +3597,17 @@ bool MainWindow::unloadTab(const QString &tabName)
return !placeholder || placeholder->expire();
}

void MainWindow::forceUnloadTab(const QString &tabName)
{
ClipboardBrowserPlaceholder *placeholder = getPlaceholder(tabName);
if (!placeholder)
return;

placeholder->unloadBrowser();

placeholder->createLoadButton();
}

MainWindow::~MainWindow()
{
disconnect();
Expand Down
18 changes: 7 additions & 11 deletions src/gui/mainwindow.h
Expand Up @@ -207,6 +207,7 @@ class MainWindow final : public QMainWindow
void setTabIcon(const QString &tabName, const QString &icon);

bool unloadTab(const QString &tabName);
void forceUnloadTab(const QString &tabName);

/**
* Save all items in tab to file.
Expand Down Expand Up @@ -333,9 +334,6 @@ class MainWindow final : public QMainWindow
/** Activate current item. */
void activateCurrentItem();

/** Set current tab. */
void setCurrentTab(const ClipboardBrowser *browser);

/** Show window and given tab and give focus to the tab. */
void showBrowser(const ClipboardBrowser *browser);

Expand Down Expand Up @@ -382,10 +380,7 @@ class MainWindow final : public QMainWindow
/** Rename current tab to given name (if possible). */
void renameTab(const QString &name, int tabIndex);

/**
* Add tab with given name if doesn't exist and focus the tab.
*/
void addTab(const QString &name);
void addAndFocusTab(const QString &name);

/** Toggle monitoring (i.e. adding new clipboard content to the first tab). */
void toggleClipboardStoring();
Expand Down Expand Up @@ -436,8 +431,8 @@ class MainWindow final : public QMainWindow
bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;

private:
ClipboardBrowser *getTabForMenu();
ClipboardBrowser *getTabForTrayMenu();
ClipboardBrowserPlaceholder *getPlaceholderForMenu();
ClipboardBrowserPlaceholder *getPlaceholderForTrayMenu();
void filterMenuItems(const QString &searchText);
void filterTrayMenuItems(const QString &searchText);
void trayActivated(QSystemTrayIcon::ActivationReason reason);
Expand Down Expand Up @@ -491,6 +486,7 @@ class MainWindow final : public QMainWindow
void moveToBottom();

void onBrowserCreated(ClipboardBrowser *browser);
void onBrowserDestroyed(ClipboardBrowserPlaceholder *placeholder);

void onItemSelectionChanged(const ClipboardBrowser *browser);
void onItemsChanged(const ClipboardBrowser *browser);
Expand Down Expand Up @@ -610,8 +606,8 @@ class MainWindow final : public QMainWindow

QAction *actionForMenuItem(int id, QWidget *parent, Qt::ShortcutContext context);

void addMenuItems(TrayMenu *menu, ClipboardBrowser *c, int maxItemCount, const QString &searchText);
void activateMenuItem(ClipboardBrowser *c, const QVariantMap &data, bool omitPaste);
void addMenuItems(TrayMenu *menu, ClipboardBrowserPlaceholder *placeholder, int maxItemCount, const QString &searchText);
void activateMenuItem(ClipboardBrowserPlaceholder *placeholder, const QVariantMap &data, bool omitPaste);
bool toggleMenu(TrayMenu *menu, QPoint pos);
bool toggleMenu(TrayMenu *menu);

Expand Down

0 comments on commit 20ec52e

Please sign in to comment.