<commit_before>/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2010-2011 Razor team
 * Authors:
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "dialog.h"
#include "ui_dialog.h"
#include "widgets.h"
#include "commanditemmodel.h"
#include "configuredialog/configuredialog.h"

#include <LXQt/Globals>
#include <LXQt/Settings>
#include <LXQt/HtmlDelegate>
#include <LXQt/PowerManager>
#include <LXQt/ScreenSaver>
#include <LXQtGlobalKeys/Action>
#include <LXQtGlobalKeys/Client>

#include <QApplication>
#include <QDebug>
#include <QCloseEvent>
#include <QGuiApplication>
#include <QProcess>
#include <QLineEdit>
#include <QAction>
#include <QMessageBox>
#include <QApplication>
#include <QMenu>
#include <QWindow>
#include <QScreen>
#include <QScrollBar>

#include <KWindowSystem/KWindowSystem>

#define DEFAULT_SHORTCUT "Alt+F2"

/************************************************

 ************************************************/
Dialog::Dialog(QWidget *parent) :
    QDialog(parent, Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint),
    ui(new Ui::Dialog),
    mSettings(new LXQt::Settings(QSL("lxqt-runner"), this)),
    mGlobalShortcut(0),
    mLockCascadeChanges(false),
    mDesktopChanged(false),
    mConfigureDialog(0)
{
    ui->setupUi(this);
    setWindowTitle(QSL("LXQt Runner"));
    setAttribute(Qt::WA_TranslucentBackground);

    connect(LXQt::Settings::globalSettings(), &LXQt::GlobalSettings::iconThemeChanged, this, qOverload<>(&Dialog::update));
    connect(ui->closeButton, &QToolButton::clicked, this, &Dialog::hide);
    connect(mSettings, &LXQt::Settings::settingsChanged, this, &Dialog::applySettings);

    mSearchTimer.setSingleShot(true);
    connect(&mSearchTimer, &QTimer::timeout, ui->commandEd, [this] {
        setFilter(ui->commandEd->text());
    });
    mSearchTimer.setInterval(250); // typing speed (not very fast)

    ui->commandEd->installEventFilter(this);

    connect(ui->commandEd, &QLineEdit::textChanged, this, [this] (QString const &) {
        mSearchTimer.start();
    });
    connect(ui->commandEd, &QLineEdit::returnPressed, this, &Dialog::runCommand);

    mCommandItemModel = new CommandItemModel(mSettings->value(QL1S("dialog/history_use"), true).toBool(), this);
    ui->commandList->installEventFilter(this);
    ui->commandList->setModel(mCommandItemModel);
    ui->commandList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->commandList, &MyListView::clicked, this, &Dialog::runCommand);
    setFilter(QString());
    dataChanged();

    ui->commandList->setItemDelegate(new LXQt::HtmlDelegate(QSize(32, 32), ui->commandList));

    // Popup menu ...............................
    QAction *a = new QAction(QIcon::fromTheme(QSL("configure")), tr("Configure"), this);
    connect(a, &QAction::triggered, this, &Dialog::showConfigDialog);
    addAction(a);

    a = new QAction(QIcon::fromTheme(QSL("edit-clear-history")), tr("Clear History"), this);
    connect(a, &QAction::triggered, mCommandItemModel, &CommandItemModel::clearHistory);
    addAction(a);

    mPowerManager = new LXQt::PowerManager(this);
    addActions(mPowerManager->availableActions());
    mScreenSaver = new LXQt::ScreenSaver(this);
    addActions(mScreenSaver->availableActions());

    setContextMenuPolicy(Qt::ActionsContextMenu);

    QMenu *menu = new QMenu(this);
    menu->addActions(actions());
    ui->actionButton->setMenu(menu);
    // End of popup menu ........................

    applySettings();

    // screen updates
    connect(qApp, &QApplication::screenAdded, this, &Dialog::realign);
    connect(qApp, &QApplication::screenRemoved, this, &Dialog::realign);
    const auto primaryScreen = QGuiApplication::primaryScreen();
    if (primaryScreen != nullptr)
        connect(primaryScreen, &QScreen::availableGeometryChanged, this, &Dialog::realign);

    connect(mGlobalShortcut, &GlobalKeyShortcut::Action::activated, this, &Dialog::showHide);
    connect(mGlobalShortcut, &GlobalKeyShortcut::Action::shortcutChanged, this, &Dialog::shortcutChanged);
    connect(KWindowSystem::self(), &KWindowSystem::activeWindowChanged, this, &Dialog::onActiveWindowChanged);
    connect(KWindowSystem::self(), &KWindowSystem::currentDesktopChanged, this, &Dialog::onCurrentDesktopChanged);

    resize(mSettings->value(QL1S("dialog/width"), 400).toInt(), size().height());

    // TEST
    connect(mCommandItemModel, &CommandItemModel::layoutChanged, this, &Dialog::dataChanged);
}


/************************************************

 ************************************************/
Dialog::~Dialog()
{
    delete ui;
}


/************************************************

 ************************************************/
void Dialog::closeEvent(QCloseEvent *event)
{
    hide();
    event->accept();
}


/************************************************

 ************************************************/
QSize Dialog::sizeHint() const
{
    QSize size = QDialog::sizeHint();
    size.setWidth(this->size().width());
    return size;
}


/************************************************

 ************************************************/
void Dialog::resizeEvent(QResizeEvent * /*event*/)
{
    mSettings->setValue(QL1S("dialog/width"), size().width());
}


/************************************************<commit_msg>No redundant writing of dialog width to config file (#206)<commit_after>/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2010-2011 Razor team
 * Authors:
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "dialog.h"
#include "ui_dialog.h"
#include "widgets.h"
#include "commanditemmodel.h"
#include "configuredialog/configuredialog.h"

#include <LXQt/Globals>
#include <LXQt/Settings>
#include <LXQt/HtmlDelegate>
#include <LXQt/PowerManager>
#include <LXQt/ScreenSaver>
#include <LXQtGlobalKeys/Action>
#include <LXQtGlobalKeys/Client>

#include <QApplication>
#include <QDebug>
#include <QCloseEvent>
#include <QGuiApplication>
#include <QProcess>
#include <QLineEdit>
#include <QAction>
#include <QMessageBox>
#include <QApplication>
#include <QMenu>
#include <QWindow>
#include <QScreen>
#include <QScrollBar>

#include <KWindowSystem/KWindowSystem>

#define DEFAULT_SHORTCUT "Alt+F2"

/************************************************

 ************************************************/
Dialog::Dialog(QWidget *parent) :
    QDialog(parent, Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint),
    ui(new Ui::Dialog),
    mSettings(new LXQt::Settings(QSL("lxqt-runner"), this)),
    mGlobalShortcut(0),
    mLockCascadeChanges(false),
    mDesktopChanged(false),
    mConfigureDialog(0)
{
    ui->setupUi(this);
    setWindowTitle(QSL("LXQt Runner"));
    setAttribute(Qt::WA_TranslucentBackground);

    connect(LXQt::Settings::globalSettings(), &LXQt::GlobalSettings::iconThemeChanged, this, qOverload<>(&Dialog::update));
    connect(ui->closeButton, &QToolButton::clicked, this, &Dialog::hide);
    connect(mSettings, &LXQt::Settings::settingsChanged, this, &Dialog::applySettings);

    mSearchTimer.setSingleShot(true);
    connect(&mSearchTimer, &QTimer::timeout, ui->commandEd, [this] {
        setFilter(ui->commandEd->text());
    });
    mSearchTimer.setInterval(250); // typing speed (not very fast)

    ui->commandEd->installEventFilter(this);

    connect(ui->commandEd, &QLineEdit::textChanged, this, [this] (QString const &) {
        mSearchTimer.start();
    });
    connect(ui->commandEd, &QLineEdit::returnPressed, this, &Dialog::runCommand);

    mCommandItemModel = new CommandItemModel(mSettings->value(QL1S("dialog/history_use"), true).toBool(), this);
    ui->commandList->installEventFilter(this);
    ui->commandList->setModel(mCommandItemModel);
    ui->commandList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->commandList, &MyListView::clicked, this, &Dialog::runCommand);
    setFilter(QString());
    dataChanged();

    ui->commandList->setItemDelegate(new LXQt::HtmlDelegate(QSize(32, 32), ui->commandList));

    // Popup menu ...............................
    QAction *a = new QAction(QIcon::fromTheme(QSL("configure")), tr("Configure"), this);
    connect(a, &QAction::triggered, this, &Dialog::showConfigDialog);
    addAction(a);

    a = new QAction(QIcon::fromTheme(QSL("edit-clear-history")), tr("Clear History"), this);
    connect(a, &QAction::triggered, mCommandItemModel, &CommandItemModel::clearHistory);
    addAction(a);

    mPowerManager = new LXQt::PowerManager(this);
    addActions(mPowerManager->availableActions());
    mScreenSaver = new LXQt::ScreenSaver(this);
    addActions(mScreenSaver->availableActions());

    setContextMenuPolicy(Qt::ActionsContextMenu);

    QMenu *menu = new QMenu(this);
    menu->addActions(actions());
    ui->actionButton->setMenu(menu);
    // End of popup menu ........................

    applySettings();

    // screen updates
    connect(qApp, &QApplication::screenAdded, this, &Dialog::realign);
    connect(qApp, &QApplication::screenRemoved, this, &Dialog::realign);
    const auto primaryScreen = QGuiApplication::primaryScreen();
    if (primaryScreen != nullptr)
        connect(primaryScreen, &QScreen::availableGeometryChanged, this, &Dialog::realign);

    connect(mGlobalShortcut, &GlobalKeyShortcut::Action::activated, this, &Dialog::showHide);
    connect(mGlobalShortcut, &GlobalKeyShortcut::Action::shortcutChanged, this, &Dialog::shortcutChanged);
    connect(KWindowSystem::self(), &KWindowSystem::activeWindowChanged, this, &Dialog::onActiveWindowChanged);
    connect(KWindowSystem::self(), &KWindowSystem::currentDesktopChanged, this, &Dialog::onCurrentDesktopChanged);

    resize(mSettings->value(QL1S("dialog/width"), 400).toInt(), size().height());

    // TEST
    connect(mCommandItemModel, &CommandItemModel::layoutChanged, this, &Dialog::dataChanged);
}


/************************************************

 ************************************************/
Dialog::~Dialog()
{
    delete ui;
}


/************************************************

 ************************************************/
void Dialog::closeEvent(QCloseEvent *event)
{
    hide();
    event->accept();
}


/************************************************

 ************************************************/
QSize Dialog::sizeHint() const
{
    QSize size = QDialog::sizeHint();
    size.setWidth(this->size().width());
    return size;
}


/************************************************

 ************************************************/
void Dialog::resizeEvent(QResizeEvent *event)
{
    if (event->spontaneous())
        mSettings->setValue(QL1S("dialog/width"), size().width());
}


/************************************************
{'fraction_non_alphanumeric': 0.2235511318028576, 'fraction_numerical': 0.006341306790817146, 'mean_word_length': 5.448757763975156, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 118, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7500758', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3544, 'n_words': 767}