Debian Codegraph @

qt5/guiwrapper/mainwindow.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2012~2013 by CSSlayer *
3 * [email protected] *
4 * Copyright (C) 2017~2017 by xzhao *
5 * [email protected] *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21
22 #include <QDebug>
23 #include <QLocale>
24 #include <QPushButton>
25 #include <libintl.h>
26
27 #include "common.h"
28 #include "fcitx-utils/utils.h"
29 #include "fcitxqtconfiguifactory.h"
30 #include "fcitxqtconnection.h"
31 #include "fcitxqtinputmethodproxy.h"
32 #include "mainwindow.h"
33
34 MainWindow::MainWindow(FcitxQtConfigUIWidget *pluginWidget, QWidget *parent)
35 : QMainWindow(parent), m_ui(new Ui::MainWindow),
36 m_connection(new FcitxQtConnection(this)), m_pluginWidget(pluginWidget),
37 m_proxy(0) {
38 m_ui->setupUi(this);
39 m_ui->verticalLayout->insertWidget(0, m_pluginWidget);
40 m_ui->buttonBox->button(QDialogButtonBox::Save)->setText(_("&Save"));
41 m_ui->buttonBox->button(QDialogButtonBox::Reset)->setText(_("&Reset"));
42 m_ui->buttonBox->button(QDialogButtonBox::Close)->setText(_("&Close"));
43 m_ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
44 m_ui->buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
45 setWindowIcon(QIcon::fromTheme(m_pluginWidget->icon()));
46 setWindowTitle(m_pluginWidget->title());
47
48 connect(m_pluginWidget, SIGNAL(changed(bool)), this, SLOT(changed(bool)));
49 if (m_pluginWidget->asyncSave())
50 connect(m_pluginWidget, SIGNAL(saveFinished()), this,
51 SLOT(saveFinished()));
52 connect(m_ui->buttonBox, SIGNAL(clicked(QAbstractButton *)), this,
53 SLOT(clicked(QAbstractButton *)));
54 connect(m_connection, SIGNAL(connected()), this, SLOT(connected()));
55
56 m_connection->startConnection();
57 }
58
59 void MainWindow::connected() {
60 if (m_proxy) {
61 delete m_proxy;
62 }
63 m_proxy = new FcitxQtInputMethodProxy(m_connection->serviceName(),
64 QLatin1String("/inputmethod"),
65 *m_connection->connection(), this);
66 }
67
68 void MainWindow::clicked(QAbstractButton *button) {
69 QDialogButtonBox::StandardButton standardButton =
70 m_ui->buttonBox->standardButton(button);
71 if (standardButton == QDialogButtonBox::Save) {
72 if (m_pluginWidget->asyncSave())
73 m_pluginWidget->setEnabled(false);
74 m_pluginWidget->save();
75 if (!m_pluginWidget->asyncSave())
76 saveFinished();
77 } else if (standardButton == QDialogButtonBox::Close) {
78 qApp->quit();
79 } else if (standardButton == QDialogButtonBox::Reset) {
80 m_pluginWidget->load();
81 }
82 }
83
84 void MainWindow::saveFinished() {
85 if (m_pluginWidget->asyncSave())
86 m_pluginWidget->setEnabled(true);
87 if (m_proxy) {
88 m_proxy->ReloadAddonConfig(m_pluginWidget->addon());
89 }
90 }
91
92 void MainWindow::changed(bool changed) {
93 m_ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(changed);
94 m_ui->buttonBox->button(QDialogButtonBox::Reset)->setEnabled(changed);
95 }
96
97 MainWindow::~MainWindow() { delete m_ui; }