1
// Copyright 2010, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#include "gui/config_dialog/roman_table_editor.h"
32
#include <QtGui/QtGui>
37
#include "base/base.h"
38
#include "base/config_file_stream.h"
39
#include "base/util.h"
40
#include "session/commands.pb.h"
48
IMPORT_FROM_FILE_INDEX = 2,
49
EXPORT_TO_FILE_INDEX = 3,
53
const char kRomanTableFile[] = "system://romanji-hiragana.tsv";
56
RomanTableEditorDialog::RomanTableEditorDialog(QWidget *parent)
57
: GenericTableEditorDialog(parent, 3) {
58
actions_.reset(new QAction * [MENU_SIZE]);
59
actions_[NEW_INDEX] = mutable_edit_menu()->addAction(tr("New entry"));
60
actions_[REMOVE_INDEX] =
61
mutable_edit_menu()->addAction(tr("Remove selected entries"));
62
mutable_edit_menu()->addSeparator();
63
actions_[IMPORT_FROM_FILE_INDEX] =
64
mutable_edit_menu()->addAction(tr("Import from file..."));
65
actions_[EXPORT_TO_FILE_INDEX] =
66
mutable_edit_menu()->addAction(tr("Export to file..."));
67
mutable_edit_menu()->addSeparator();
68
actions_[RESET_INDEX] =
69
mutable_edit_menu()->addAction(tr("Reset to defaults"));
71
setWindowTitle(tr("Mozc Romaji table editor"));
72
CHECK(mutable_table_widget());
73
CHECK_EQ(mutable_table_widget()->columnCount(), 3);
75
headers << tr("Input") << tr("Output") << tr("Next input");
76
mutable_table_widget()->setHorizontalHeaderLabels(headers);
83
RomanTableEditorDialog::~RomanTableEditorDialog() {}
85
string RomanTableEditorDialog::GetDefaultRomanTable() {
86
scoped_ptr<istream> ifs(ConfigFileStream::Open(kRomanTableFile));
87
CHECK(ifs.get() != NULL); // should never happen
89
vector<string> fields;
90
while (getline(*ifs.get(), line)) {
94
Util::ChopReturns(&line);
96
Util::SplitStringAllowEmpty(line, "\t", &fields);
97
if (fields.size() < 2) {
98
VLOG(3) << "field size < 2";
104
if (fields.size() >= 3) {
113
bool RomanTableEditorDialog::LoadFromStream(istream *is) {
116
vector<string> fields;
117
mutable_table_widget()->setRowCount(0);
118
mutable_table_widget()->verticalHeader()->hide();
121
while (getline(*is, line)) {
125
Util::ChopReturns(&line);
128
Util::SplitStringAllowEmpty(line, "\t", &fields);
129
if (fields.size() < 2) {
130
VLOG(3) << "field size < 2";
134
if (fields.size() == 2) {
135
fields.push_back("");
138
QTableWidgetItem *input
139
= new QTableWidgetItem(QString::fromUtf8(fields[0].c_str()));
140
QTableWidgetItem *output
141
= new QTableWidgetItem(QString::fromUtf8(fields[1].c_str()));
142
QTableWidgetItem *pending
143
= new QTableWidgetItem(QString::fromUtf8(fields[2].c_str()));
145
mutable_table_widget()->insertRow(row);
146
mutable_table_widget()->setItem(row, 0, input);
147
mutable_table_widget()->setItem(row, 1, output);
148
mutable_table_widget()->setItem(row, 2, pending);
151
if (row >= GenericTableEditorDialog::max_entry_size()) {
152
QMessageBox::warning(
155
tr("You can't have more than %1 entries").arg(
156
GenericTableEditorDialog::max_entry_size()));
166
bool RomanTableEditorDialog::LoadDefaultRomanTable() {
167
scoped_ptr<istream> ifs(ConfigFileStream::Open(kRomanTableFile));
168
CHECK(ifs.get() != NULL); // should never happen
169
CHECK(LoadFromStream(ifs.get()));
173
bool RomanTableEditorDialog::Update() {
174
if (mutable_table_widget()->rowCount() == 0) {
175
QMessageBox::warning(this,
177
tr("Romaji to Kana table is empty."));
181
string *table = mutable_table();
183
for (int i = 0; i < mutable_table_widget()->rowCount(); ++i) {
185
mutable_table_widget()->item(i, 0)->text().toStdString();
186
const string output =
187
mutable_table_widget()->item(i, 1)->text().toStdString();
188
const string pending =
189
mutable_table_widget()->item(i, 2)->text().toStdString();
190
if (input.empty() || (output.empty() && pending.empty())) {
196
if (!pending.empty()) {
206
void RomanTableEditorDialog::UpdateMenuStatus() {
207
const bool status = (mutable_table_widget()->rowCount() > 0);
208
actions_[RESET_INDEX]->setEnabled(status);
209
actions_[REMOVE_INDEX]->setEnabled(status);
210
UpdateOKButton(status);
213
void RomanTableEditorDialog::OnEditMenuAction(QAction *action) {
214
if (action == actions_[NEW_INDEX]) {
216
} else if (action == actions_[REMOVE_INDEX]) {
217
DeleteSelectedItems();
218
} else if (action == actions_[IMPORT_FROM_FILE_INDEX] ||
219
action == actions_[RESET_INDEX]) { // import or reset
220
if (mutable_table_widget()->rowCount() > 0 &&
222
QMessageBox::question(
225
tr("Do you want to overwrite the current roman table?"),
226
QMessageBox::Ok | QMessageBox::Cancel,
227
QMessageBox::Cancel)) {
231
if (action == actions_[IMPORT_FROM_FILE_INDEX]) {
233
} else if (action == actions_[RESET_INDEX]) {
234
LoadDefaultRomanTable();
236
} else if (action == actions_[EXPORT_TO_FILE_INDEX]) {
244
bool RomanTableEditorDialog::Show(QWidget *parent,
245
const string ¤t_roman_table,
246
string *new_roman_table) {
247
RomanTableEditorDialog window(parent);
248
if (current_roman_table.empty()) {
249
window.LoadDefaultRomanTable();
251
window.LoadFromString(current_roman_table);
255
const bool result = (QDialog::Accepted == window.exec());
256
new_roman_table->clear();
259
window.table() != window.GetDefaultRomanTable()) {
260
*new_roman_table = window.table();