File: mainwindow.cpp

package info (click to toggle)
fcitx5-qt 5.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,336 kB
  • sloc: cpp: 11,663; xml: 243; ansic: 46; makefile: 14; sh: 9
file content (159 lines) | stat: -rw-r--r-- 5,155 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
 * SPDX-FileCopyrightText: 2017~2017 xzhao <i@xuzhao.net>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "mainwindow.h"
#include "fcitxqtconfiguifactory.h"
#include "fcitxqtcontrollerproxy.h"
#include "fcitxqtwatcher.h"
#include <QCloseEvent>
#include <QDebug>
#include <QLocale>
#include <QMessageBox>
#include <QPushButton>
#include <QShowEvent>
#include <QWidget>
#include <QWindow>
#include <fcitx-utils/i18n.h>

namespace fcitx {

MainWindow::MainWindow(const QString &path, FcitxQtConfigUIWidget *pluginWidget,
                       QWidget *parent)
    : QWidget(parent), path_(path), watcher_(new FcitxQtWatcher(this)),
      pluginWidget_(pluginWidget), proxy_(0) {
    setupUi(this);
    watcher_->setConnection(QDBusConnection::sessionBus());
    verticalLayout->insertWidget(0, pluginWidget_);
    buttonBox->button(QDialogButtonBox::Ok)->setText(_("&Ok"));
    buttonBox->button(QDialogButtonBox::Apply)->setText(_("&Apply"));
    buttonBox->button(QDialogButtonBox::Reset)->setText(_("&Reset"));
    buttonBox->button(QDialogButtonBox::Close)->setText(_("&Close"));
    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
    buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
    setWindowIcon(QIcon::fromTheme(pluginWidget_->icon()));
    setWindowTitle(pluginWidget_->title());

    connect(pluginWidget_, &FcitxQtConfigUIWidget::changed, this,
            &MainWindow::changed);
    if (pluginWidget_->asyncSave()) {
        connect(pluginWidget_, &FcitxQtConfigUIWidget::saveFinished, this,
                &MainWindow::saveFinished);
    }
    connect(pluginWidget_, &FcitxQtConfigUIWidget::saveSubConfig, this,
            &MainWindow::saveSubConfig);
    connect(buttonBox, &QDialogButtonBox::clicked, this, &MainWindow::clicked);
    connect(watcher_, &FcitxQtWatcher::availabilityChanged, this,
            &MainWindow::availabilityChanged);

    watcher_->watch();
}

void MainWindow::availabilityChanged(bool avail) {
    if (!avail) {
        return;
    }
    if (proxy_) {
        delete proxy_;
    }
    proxy_ = new FcitxQtControllerProxy(watcher_->serviceName(),
                                        QLatin1String("/controller"),
                                        watcher_->connection(), this);
}

void MainWindow::clicked(QAbstractButton *button) {
    QDialogButtonBox::StandardButton standardButton =
        buttonBox->standardButton(button);
    if (standardButton == QDialogButtonBox::Apply ||
        standardButton == QDialogButtonBox::Ok) {
        if (pluginWidget_->asyncSave())
            pluginWidget_->setEnabled(false);
        if (standardButton == QDialogButtonBox::Ok) {
            closeAfterSave_ = true;
        }
        pluginWidget_->save();
        if (!pluginWidget_->asyncSave())
            saveFinished();
    } else if (standardButton == QDialogButtonBox::Close) {
        qApp->quit();
    } else if (standardButton == QDialogButtonBox::Reset) {
        pluginWidget_->load();
    }
}

void MainWindow::saveFinished() {
    if (proxy_) {
        // Pass some arbitrary thing.
        auto watcher = new QDBusPendingCallWatcher(
            proxy_->SetConfig(path_, QDBusVariant(0)), this);
        connect(watcher, &QDBusPendingCallWatcher::finished, this,
                &MainWindow::saveFinishedPhase2);
    } else {
        saveFinishedPhase2(nullptr);
    }
}

void MainWindow::saveFinishedPhase2(QDBusPendingCallWatcher *watcher) {
    if (watcher) {
        watcher->deleteLater();
    }
    if (pluginWidget_->asyncSave()) {
        pluginWidget_->setEnabled(true);
    }
    if (!watcher || watcher->isError()) {
        QMessageBox::warning(
            this, _("Failed to notify Fcitx"),
            _("Failed to notify Fcitx about the configuration change."));
        closeAfterSave_ = false;
        return;
    }
    if (closeAfterSave_) {
        qApp->quit();
    }
}

void MainWindow::saveSubConfig(const QString &path) {
    if (proxy_) {
        // Pass some arbitrary thing.
        proxy_->SetConfig(path, QDBusVariant(0));
    }
}

void MainWindow::changed(bool changed) {
    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(changed);
    buttonBox->button(QDialogButtonBox::Apply)->setEnabled(changed);
    buttonBox->button(QDialogButtonBox::Reset)->setEnabled(changed);
}

void MainWindow::setParentWindow(WId id) { wid_ = id; }

void MainWindow::showEvent(QShowEvent *event) {
    if (!wid_) {
        return;
    }
    setAttribute(Qt::WA_NativeWindow, true);
    QWindow *subWindow = windowHandle();
    Q_ASSERT(subWindow);

    QWindow *mainWindow = QWindow::fromWinId(wid_);
    wid_ = 0;
    if (!mainWindow) {
        // foreign windows not supported on all platforms
        return;
    }
    connect(this, &QObject::destroyed, mainWindow, &QObject::deleteLater);
    subWindow->setTransientParent(mainWindow);

    QWidget::showEvent(event);
}

void MainWindow::closeEvent(QCloseEvent *event) {
    QWidget::closeEvent(event);

    qApp->quit();
}
} // namespace fcitx