| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2012~2012 by CSSlayer * |
| 3 |
* [email protected] * |
| 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 <libintl.h> |
| 21 |
#include <fcitx-config/xdg.h> |
| 22 |
|
| 23 |
#include <QFileDialog> |
| 24 |
#include <QMessageBox> |
| 25 |
#include <QCloseEvent> |
| 26 |
#include <QDebug> |
| 27 |
|
| 28 |
#include "editor.h" |
| 29 |
#include "model.h" |
| 30 |
#include "dialog.h" |
| 31 |
#include "mactab.h" |
| 32 |
#include "ui_editor.h" |
| 33 |
#include "common.h" |
| 34 |
|
| 35 |
namespace fcitx_unikey { |
| 36 |
|
| 37 |
MacroEditor::MacroEditor(QWidget* parent): FcitxQtConfigUIWidget(parent) |
| 38 |
,m_ui(new Ui::Editor) |
| 39 |
{ |
| 40 |
m_ui->setupUi(this); |
| 41 |
m_ui->addButton->setText(_("&Add")); |
| 42 |
m_ui->deleteButton->setText(_("&Delete")); |
| 43 |
m_ui->clearButton->setText(_("De&lete All")); |
| 44 |
m_ui->importButton->setText(_("&Import")); |
| 45 |
m_ui->exportButton->setText(_("&Export")); |
| 46 |
m_ui->macroTableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 47 |
m_ui->macroTableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 48 |
setWindowTitle(_("Unikey Macro Editor")); |
| 49 |
|
| 50 |
connect(m_ui->addButton, SIGNAL(clicked(bool)), this, SLOT(addWord())); |
| 51 |
connect(m_ui->deleteButton, SIGNAL(clicked(bool)), this, SLOT(deleteWord())); |
| 52 |
connect(m_ui->clearButton, SIGNAL(clicked(bool)), this, SLOT(deleteAllWord())); |
| 53 |
connect(m_ui->importButton, SIGNAL(clicked(bool)), this, SLOT(importMacro())); |
| 54 |
connect(m_ui->exportButton, SIGNAL(clicked(bool)), this, SLOT(exportMacro())); |
| 55 |
load(); |
| 56 |
itemFocusChanged(); |
| 57 |
} |
| 58 |
|
| 59 |
MacroEditor::~MacroEditor() |
| 60 |
{ |
| 61 |
delete m_ui; |
| 62 |
} |
| 63 |
|
| 64 |
QString MacroEditor::addon() |
| 65 |
{ |
| 66 |
return "fcitx-unikey"; |
| 67 |
} |
| 68 |
|
| 69 |
QString MacroEditor::icon() |
| 70 |
{ |
| 71 |
return "fcitx-unikey"; |
| 72 |
} |
| 73 |
|
| 74 |
QString MacroEditor::title() |
| 75 |
{ |
| 76 |
return _("Unikey Macro Editor"); |
| 77 |
} |
| 78 |
|
| 79 |
void MacroEditor::itemFocusChanged() |
| 80 |
{ |
| 81 |
m_ui->deleteButton->setEnabled(m_ui->macroTableView->currentIndex().isValid()); |
| 82 |
} |
| 83 |
|
| 84 |
void MacroEditor::deleteWord() |
| 85 |
{ |
| 86 |
if (!m_ui->macroTableView->currentIndex().isValid()) |
| 87 |
return; |
| 88 |
int row = m_ui->macroTableView->currentIndex().row(); |
| 89 |
m_model->deleteItem(row); |
| 90 |
} |
| 91 |
|
| 92 |
void MacroEditor::deleteAllWord() |
| 93 |
{ |
| 94 |
m_model->deleteAllItem(); |
| 95 |
} |
| 96 |
|
| 97 |
void MacroEditor::addWord() |
| 98 |
{ |
| 99 |
MacroDialog* dialog = new MacroDialog(this); |
| 100 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 101 |
dialog->open(); |
| 102 |
connect(dialog, SIGNAL(accepted()), this, SLOT(addWordAccepted())); |
| 103 |
} |
| 104 |
|
| 105 |
QString MacroEditor::getData(CMacroTable* table, int i, bool iskey) { |
| 106 |
|
| 107 |
char key[MAX_MACRO_KEY_LEN*3]; |
| 108 |
char value[MAX_MACRO_TEXT_LEN*3]; |
| 109 |
do { |
| 110 |
if (i < table->getCount()) { |
| 111 |
const StdVnChar* p = NULL; |
| 112 |
int maxOutLen = 0; |
| 113 |
const char* result = NULL; |
| 114 |
if (iskey) { |
| 115 |
p = table->getKey(i); |
| 116 |
maxOutLen = sizeof(key); |
| 117 |
result = key; |
| 118 |
} else { |
| 119 |
p = table->getText(i); |
| 120 |
maxOutLen = sizeof(value); |
| 121 |
result = value; |
| 122 |
} |
| 123 |
|
| 124 |
if (!p) |
| 125 |
break; |
| 126 |
int inLen = -1; |
| 127 |
int ret = VnConvert(CONV_CHARSET_VNSTANDARD, CONV_CHARSET_XUTF8, |
| 128 |
(UKBYTE*) p, (UKBYTE*)result, |
| 129 |
&inLen, &maxOutLen); |
| 130 |
if (ret != 0) |
| 131 |
break; |
| 132 |
return QString::fromUtf8(result); |
| 133 |
} |
| 134 |
} while(0); |
| 135 |
return QString(); |
| 136 |
} |
| 137 |
|
| 138 |
void MacroEditor::addWordAccepted() |
| 139 |
{ |
| 140 |
const MacroDialog* dialog = qobject_cast< const MacroDialog* >(QObject::sender()); |
| 141 |
|
| 142 |
m_model->addItem(dialog->macro(), dialog->word()); |
| 143 |
} |
| 144 |
|
| 145 |
void MacroEditor::load() |
| 146 |
{ |
| 147 |
m_table = new CMacroTable; |
| 148 |
m_table->init(); |
| 149 |
char* fileName; |
| 150 |
FcitxXDGGetFileUserWithPrefix("unikey", "macro", NULL, &fileName); |
| 151 |
m_table->loadFromFile(fileName); |
| 152 |
free(fileName); |
| 153 |
m_model = new MacroModel(this); |
| 154 |
m_model->load(m_table); |
| 155 |
m_ui->macroTableView->horizontalHeader()->setStretchLastSection(true); |
| 156 |
m_ui->macroTableView->verticalHeader()->setVisible(false); |
| 157 |
m_ui->macroTableView->setModel(m_model); |
| 158 |
connect(m_ui->macroTableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(itemFocusChanged())); |
| 159 |
connect(m_model, SIGNAL(needSaveChanged(bool)), this, SIGNAL(changed(bool))); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
void MacroEditor::save() |
| 164 |
{ |
| 165 |
m_model->save(m_table); |
| 166 |
FILE* f = FcitxXDGGetFileUserWithPrefix("unikey", "macro", "w", NULL); |
| 167 |
m_table->writeToFp(f); |
| 168 |
} |
| 169 |
|
| 170 |
void MacroEditor::importMacro() |
| 171 |
{ |
| 172 |
QFileDialog* dialog = new QFileDialog(this); |
| 173 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 174 |
dialog->setFileMode(QFileDialog::ExistingFile); |
| 175 |
dialog->setAcceptMode(QFileDialog::AcceptOpen); |
| 176 |
dialog->open(); |
| 177 |
connect(dialog, SIGNAL(accepted()), this, SLOT(importFileSelected())); |
| 178 |
} |
| 179 |
|
| 180 |
void MacroEditor::importFileSelected() |
| 181 |
{ |
| 182 |
const QFileDialog* dialog = qobject_cast< const QFileDialog* >(QObject::sender()); |
| 183 |
qDebug() << dialog->selectedFiles(); |
| 184 |
} |
| 185 |
|
| 186 |
void MacroEditor::exportMacro() |
| 187 |
{ |
| 188 |
QFileDialog* dialog = new QFileDialog(this); |
| 189 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 190 |
dialog->setDirectory("macro"); |
| 191 |
dialog->setAcceptMode(QFileDialog::AcceptSave); |
| 192 |
dialog->open(); |
| 193 |
connect(dialog, SIGNAL(accepted()), this, SLOT(exportFileSelected())); |
| 194 |
} |
| 195 |
|
| 196 |
void MacroEditor::exportFileSelected() |
| 197 |
{ |
| 198 |
const QFileDialog* dialog = qobject_cast< const QFileDialog* >(QObject::sender()); |
| 199 |
if (dialog->selectedFiles().length() <= 0) |
| 200 |
return; |
| 201 |
QString file = dialog->selectedFiles()[0]; |
| 202 |
m_table->writeToFile(file.toUtf8().data()); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
} |