~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to gui/config_dialog/roman_table_editor.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2010, Google Inc.
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
//
 
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
 
13
// distribution.
 
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.
 
17
//
 
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.
 
29
 
 
30
#include "gui/config_dialog/roman_table_editor.h"
 
31
 
 
32
#include <QtGui/QtGui>
 
33
#include <cctype>
 
34
#include <sstream>
 
35
#include <string>
 
36
#include <vector>
 
37
#include "base/base.h"
 
38
#include "base/config_file_stream.h"
 
39
#include "base/util.h"
 
40
#include "session/commands.pb.h"
 
41
 
 
42
namespace mozc {
 
43
namespace gui {
 
44
namespace {
 
45
enum {
 
46
  NEW_INDEX              = 0,
 
47
  REMOVE_INDEX           = 1,
 
48
  IMPORT_FROM_FILE_INDEX = 2,
 
49
  EXPORT_TO_FILE_INDEX   = 3,
 
50
  RESET_INDEX            = 4,
 
51
  MENU_SIZE              = 5
 
52
};
 
53
const char kRomanTableFile[] = "system://romanji-hiragana.tsv";
 
54
}  // namespace
 
55
 
 
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"));
 
70
 
 
71
  setWindowTitle(tr("Mozc Romaji table editor"));
 
72
  CHECK(mutable_table_widget());
 
73
  CHECK_EQ(mutable_table_widget()->columnCount(), 3);
 
74
  QStringList headers;
 
75
  headers << tr("Input") << tr("Output") << tr("Next input");
 
76
  mutable_table_widget()->setHorizontalHeaderLabels(headers);
 
77
 
 
78
  resize(330, 350);
 
79
 
 
80
  UpdateMenuStatus();
 
81
}
 
82
 
 
83
RomanTableEditorDialog::~RomanTableEditorDialog() {}
 
84
 
 
85
string RomanTableEditorDialog::GetDefaultRomanTable() {
 
86
  scoped_ptr<istream> ifs(ConfigFileStream::Open(kRomanTableFile));
 
87
  CHECK(ifs.get() != NULL);  // should never happen
 
88
  string line, result;
 
89
  vector<string> fields;
 
90
  while (getline(*ifs.get(), line)) {
 
91
    if (line.empty()) {
 
92
      continue;
 
93
    }
 
94
    Util::ChopReturns(&line);
 
95
    fields.clear();
 
96
    Util::SplitStringAllowEmpty(line, "\t", &fields);
 
97
    if (fields.size() < 2) {
 
98
      VLOG(3) << "field size < 2";
 
99
      continue;
 
100
    }
 
101
    result += fields[0];
 
102
    result += '\t';
 
103
    result += fields[1];
 
104
    if (fields.size() >= 3) {
 
105
      result += '\t';
 
106
      result += fields[2];
 
107
    }
 
108
    result += '\n';
 
109
  }
 
110
  return result;
 
111
}
 
112
 
 
113
bool RomanTableEditorDialog::LoadFromStream(istream *is) {
 
114
  CHECK(is);
 
115
  string line;
 
116
  vector<string> fields;
 
117
  mutable_table_widget()->setRowCount(0);
 
118
  mutable_table_widget()->verticalHeader()->hide();
 
119
 
 
120
  int row = 0;
 
121
  while (getline(*is, line)) {
 
122
    if (line.empty()) {
 
123
      continue;
 
124
    }
 
125
    Util::ChopReturns(&line);
 
126
 
 
127
    fields.clear();
 
128
    Util::SplitStringAllowEmpty(line, "\t", &fields);
 
129
    if (fields.size() < 2) {
 
130
      VLOG(3) << "field size < 2";
 
131
      continue;
 
132
    }
 
133
 
 
134
    if (fields.size() == 2) {
 
135
      fields.push_back("");
 
136
    }
 
137
 
 
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()));
 
144
 
 
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);
 
149
    ++row;
 
150
 
 
151
    if (row >= GenericTableEditorDialog::max_entry_size()) {
 
152
      QMessageBox::warning(
 
153
          this,
 
154
          tr("Mozc settings"),
 
155
          tr("You can't have more than %1 entries").arg(
 
156
              GenericTableEditorDialog::max_entry_size()));
 
157
      break;
 
158
    }
 
159
  }
 
160
 
 
161
  UpdateMenuStatus();
 
162
 
 
163
  return true;
 
164
}
 
165
 
 
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()));
 
170
  return true;
 
171
}
 
172
 
 
173
bool RomanTableEditorDialog::Update() {
 
174
  if (mutable_table_widget()->rowCount() == 0) {
 
175
    QMessageBox::warning(this,
 
176
                         tr("Mozc settings"),
 
177
                         tr("Romaji to Kana table is empty."));
 
178
    return false;
 
179
  }
 
180
 
 
181
  string *table = mutable_table();
 
182
  table->clear();
 
183
  for (int i = 0; i < mutable_table_widget()->rowCount(); ++i) {
 
184
    const string input =
 
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())) {
 
191
      continue;
 
192
    }
 
193
    *table += input;
 
194
    *table += '\t';
 
195
    *table += output;
 
196
    if (!pending.empty()) {
 
197
      *table += '\t';
 
198
      *table += pending;
 
199
    }
 
200
    *table += '\n';
 
201
  }
 
202
 
 
203
  return true;
 
204
}
 
205
 
 
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);
 
211
}
 
212
 
 
213
void RomanTableEditorDialog::OnEditMenuAction(QAction *action) {
 
214
  if (action == actions_[NEW_INDEX]) {
 
215
    AddNewItem();
 
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 &&
 
221
        QMessageBox::Ok !=
 
222
        QMessageBox::question(
 
223
            this,
 
224
            tr("Mozc settings"),
 
225
            tr("Do you want to overwrite the current roman table?"),
 
226
            QMessageBox::Ok | QMessageBox::Cancel,
 
227
            QMessageBox::Cancel)) {
 
228
      return;
 
229
    }
 
230
 
 
231
    if (action == actions_[IMPORT_FROM_FILE_INDEX]) {
 
232
      Import();
 
233
    } else if (action == actions_[RESET_INDEX]) {
 
234
      LoadDefaultRomanTable();
 
235
    }
 
236
  } else if (action == actions_[EXPORT_TO_FILE_INDEX]) {
 
237
    Export();
 
238
  }
 
239
 
 
240
  return;
 
241
}
 
242
 
 
243
// static
 
244
bool RomanTableEditorDialog::Show(QWidget *parent,
 
245
                                  const string &current_roman_table,
 
246
                                  string *new_roman_table) {
 
247
  RomanTableEditorDialog window(parent);
 
248
  if (current_roman_table.empty()) {
 
249
    window.LoadDefaultRomanTable();
 
250
  } else {
 
251
    window.LoadFromString(current_roman_table);
 
252
  }
 
253
 
 
254
  // open modal mode
 
255
  const bool result = (QDialog::Accepted == window.exec());
 
256
  new_roman_table->clear();
 
257
 
 
258
  if (result &&
 
259
      window.table() != window.GetDefaultRomanTable()) {
 
260
    *new_roman_table = window.table();
 
261
  }
 
262
 
 
263
  return result;
 
264
}
 
265
}  // namespace gui
 
266
}  // namespace mozc