1 /***************************************************************************
2 * Copyright (C) 2013~2013 by CSSlayer *
3 * wengxt@gmail.com *
4 * *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 * *
18 ***************************************************************************/
19
20 #include "common.h"
21 #include "dictwidget.h"
22 #include "adddictdialog.h"
23 #include "dictmodel.h"
24 #include "rulemodel.h"
25 #include "ui_dictwidget.h"
26 #include <fcitx-config/xdg.h>
27
SkkDictWidget(QWidget * parent)28 SkkDictWidget::SkkDictWidget(QWidget* parent): FcitxQtConfigUIWidget(parent)
29 ,m_ui(new Ui::SkkDictWidget)
30 {
31 m_ui->setupUi(this);
32 m_dictModel = new DictModel(this);
33 m_ruleModel = new RuleModel(this);
34 m_ui->ruleLabel->setText(_("&Rule:"));
35
36 m_ui->dictionaryView->setModel(m_dictModel);
37 m_ui->ruleComboBox->setModel(m_ruleModel);
38
39 connect(m_ui->addDictButton, SIGNAL(clicked(bool)), this, SLOT(addDictClicked()));
40 connect(m_ui->defaultDictButton, SIGNAL(clicked(bool)), this, SLOT(defaultDictClicked()));
41 connect(m_ui->removeDictButton, SIGNAL(clicked(bool)), this, SLOT(removeDictClicked()));
42 connect(m_ui->moveUpDictButton, SIGNAL(clicked(bool)), this, SLOT(moveUpDictClicked()));
43 connect(m_ui->moveDownDictButton, SIGNAL(clicked(bool)), this, SLOT(moveDownClicked()));
44 connect(m_ui->ruleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(ruleChanged(int)));
45
46 load();
47 }
48
~SkkDictWidget()49 SkkDictWidget::~SkkDictWidget()
50 {
51 delete m_ui;
52 }
53
addon()54 QString SkkDictWidget::addon()
55 {
56 return "fcitx-skk";
57 }
58
title()59 QString SkkDictWidget::title()
60 {
61 return _("Dictionary Manager");
62 }
63
icon()64 QString SkkDictWidget::icon()
65 {
66 return "fcitx-skk";
67 }
68
load()69 void SkkDictWidget::load()
70 {
71 m_dictModel->load();
72
73 FILE* fp = FcitxXDGGetFileWithPrefix("skk", "rule", "r", NULL);
74
75 QString sline;
76 do {
77 if (!fp) {
78 break;
79 }
80
81 QFile f;
82 QByteArray line;
83 if (f.open(fp, QIODevice::ReadOnly)) {;
84 line = f.readLine();
85 f.close();
86 }
87 fclose(fp);
88
89 sline = QString::fromUtf8(line).trimmed();
90
91 if (sline.isEmpty()) {
92 sline = "default";
93 }
94 } while(0);
95 m_ruleModel->load();
96 int idx = m_ruleModel->findRule(sline);
97 idx = idx < 0 ? 0 : idx;
98 m_ui->ruleComboBox->setCurrentIndex(idx);
99 Q_EMIT changed(false);
100 }
101
save()102 void SkkDictWidget::save()
103 {
104 m_dictModel->save();
105 QString name = m_ruleModel->data(m_ruleModel->index(m_ui->ruleComboBox->currentIndex(), 0), Qt::UserRole).toString();
106 FILE* fp = FcitxXDGGetFileUserWithPrefix("skk", "rule", "w", NULL);
107 if (!fp) {
108 return;
109 }
110
111 QFile f;
112 if (f.open(fp, QIODevice::WriteOnly)) {
113 f.write(name.toUtf8());
114 f.close();
115 }
116
117 fclose(fp);
118 Q_EMIT changed(false);
119 }
120
addDictClicked()121 void SkkDictWidget::addDictClicked()
122 {
123 AddDictDialog dialog;
124 int result = dialog.exec();
125 if (result == QDialog::Accepted) {
126 m_dictModel->add(dialog.dictionary());
127 Q_EMIT changed(true);
128 }
129 }
130
defaultDictClicked()131 void SkkDictWidget::defaultDictClicked()
132 {
133 m_dictModel->defaults();
134 Q_EMIT changed(true);
135 }
136
removeDictClicked()137 void SkkDictWidget::removeDictClicked()
138 {
139 if (m_ui->dictionaryView->currentIndex().isValid()) {
140 m_dictModel->removeRow(m_ui->dictionaryView->currentIndex().row());
141 Q_EMIT changed(true);
142 }
143 }
144
moveUpDictClicked()145 void SkkDictWidget::moveUpDictClicked()
146 {
147 int row = m_ui->dictionaryView->currentIndex().row();
148 if (m_dictModel->moveUp(m_ui->dictionaryView->currentIndex())) {
149 m_ui->dictionaryView->selectionModel()->setCurrentIndex(
150 m_dictModel->index(row - 1), QItemSelectionModel::ClearAndSelect);
151 Q_EMIT changed(true);
152 }
153 }
154
moveDownClicked()155 void SkkDictWidget::moveDownClicked()
156 {
157 int row = m_ui->dictionaryView->currentIndex().row();
158 if (m_dictModel->moveDown(m_ui->dictionaryView->currentIndex())) {
159 m_ui->dictionaryView->selectionModel()->setCurrentIndex(
160 m_dictModel->index(row + 1), QItemSelectionModel::ClearAndSelect);
161 Q_EMIT changed(true);
162 }
163 }
164
ruleChanged(int)165 void SkkDictWidget::ruleChanged(int )
166 {
167 Q_EMIT changed(true);
168 }
169