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

« back to all changes in this revision

Viewing changes to gui/config_dialog/character_form_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 <QtGui/QtGui>
 
31
#include "base/base.h"
 
32
#include "gui/config_dialog/character_form_editor.h"
 
33
#include "gui/config_dialog/combobox_delegate.h"
 
34
#include "session/config_handler.h"
 
35
 
 
36
namespace mozc {
 
37
namespace gui {
 
38
 
 
39
namespace {
 
40
QString FormToString(config::Config::CharacterForm form) {
 
41
  switch (form) {
 
42
    case config::Config::FULL_WIDTH:
 
43
      return QObject::tr("Fullwidth");
 
44
      break;
 
45
    case config::Config::HALF_WIDTH:
 
46
      return QObject::tr("Halfwidth");
 
47
      break;
 
48
    case config::Config::LAST_FORM:
 
49
      return QObject::tr("Remember");
 
50
      break;
 
51
    default:
 
52
      return QObject::tr("Unknown");
 
53
      break;
 
54
  }
 
55
}
 
56
 
 
57
config::Config::CharacterForm StringToForm(const QString &str) {
 
58
  if (str == QObject::tr("Fullwidth")) {
 
59
    return config::Config::FULL_WIDTH;
 
60
  } else if (str == QObject::tr("Halfwidth")) {
 
61
    return config::Config::HALF_WIDTH;
 
62
  } else if (str == QObject::tr("Remember")) {
 
63
    return config::Config::LAST_FORM;
 
64
  }
 
65
  return config::Config::FULL_WIDTH;  // failsafe
 
66
}
 
67
 
 
68
QString GroupToString(const string &str) {
 
69
  // if (str == "ア") {
 
70
  if (str == "\xE3\x82\xA2") {
 
71
    return QObject::tr("Katakana");
 
72
  } else if (str == "0") {
 
73
    return QObject::tr("Numbers");
 
74
  } else if (str == "A") {
 
75
    return QObject::tr("Alphabets");
 
76
  }
 
77
  return QString::fromUtf8(str.c_str());
 
78
}
 
79
 
 
80
const string StringToGroup(const QString &str) {
 
81
  if (str == QObject::tr("Katakana")) {
 
82
    // return "ア";
 
83
    return "\xE3\x82\xA2";
 
84
  } else if (str == QObject::tr("Numbers")) {
 
85
    return "0";
 
86
  } else if (str == QObject::tr("Alphabets")) {
 
87
    return "A";
 
88
  }
 
89
  return str.toUtf8().data();
 
90
}
 
91
}
 
92
 
 
93
CharacterFormEditor::CharacterFormEditor(QWidget *parent)
 
94
    : QTableWidget(parent), delegate_(new ComboBoxDelegate) {
 
95
  QStringList item_list;
 
96
  item_list << tr("Fullwidth") << tr("Halfwidth") << tr("Remember");
 
97
  delegate_->SetItemList(item_list);
 
98
  setEditTriggers(QAbstractItemView::AllEditTriggers);
 
99
  setItemDelegate(delegate_.get());
 
100
  setToolTip(tr("Character form editor"));
 
101
  setColumnCount(3);
 
102
  setAlternatingRowColors(true);
 
103
  setSelectionMode(QAbstractItemView::SingleSelection);
 
104
  setSelectionBehavior(QAbstractItemView::SelectItems);
 
105
  verticalHeader()->hide();
 
106
#ifdef OS_MACOSX
 
107
  // grid is basically hidden in mac ui.
 
108
  // Please take a look at iTunes.
 
109
  setShowGrid(false);
 
110
#endif
 
111
}
 
112
 
 
113
CharacterFormEditor::~CharacterFormEditor() {}
 
114
 
 
115
void CharacterFormEditor::Load(const config::Config &config) {
 
116
  clear();
 
117
  QStringList header;
 
118
  header << tr("Group") << tr("Composition") << tr("Conversion");
 
119
  setHorizontalHeaderLabels(header);
 
120
 
 
121
  scoped_ptr<config::Config> default_config;
 
122
  const config::Config *target_config = &config;
 
123
 
 
124
  // make sure that table isn't empty.
 
125
  if (config.character_form_rules_size() == 0) {
 
126
    default_config.reset(new config::Config);
 
127
    config::ConfigHandler::GetDefaultConfig(default_config.get());
 
128
    target_config = default_config.get();
 
129
  }
 
130
 
 
131
  setRowCount(0);
 
132
  setRowCount(target_config->character_form_rules_size());
 
133
 
 
134
  for (size_t row = 0;
 
135
       row < target_config->character_form_rules_size(); ++row) {
 
136
    const config::Config::CharacterFormRule &rule
 
137
        = target_config->character_form_rules(row);
 
138
    const QString group = GroupToString(rule.group());
 
139
    const QString preedit = FormToString(rule.preedit_character_form());
 
140
    const QString conversion = FormToString(rule.conversion_character_form());
 
141
 
 
142
    QTableWidgetItem *item_group = new QTableWidgetItem(group);
 
143
    item_group->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
 
144
    QTableWidgetItem *item_preedit = new QTableWidgetItem(preedit);
 
145
    item_preedit->setTextAlignment(Qt::AlignCenter);
 
146
    QTableWidgetItem *item_conversion = new QTableWidgetItem(conversion);
 
147
    item_conversion->setTextAlignment(Qt::AlignCenter);
 
148
 
 
149
    // Preedit Katakan is always FULLWIDTH
 
150
    // This item should not be editable
 
151
    if (group == QObject::tr("Katakana")) {
 
152
      item_preedit->setFlags(0);   // disable flag
 
153
    }
 
154
 
 
155
    setItem(row, 0, item_group);
 
156
    setItem(row, 1, item_preedit);
 
157
    setItem(row, 2, item_conversion);
 
158
    const int height = rowHeight(row);
 
159
    setRowHeight(row, static_cast<int>(height * 0.7));
 
160
  }
 
161
 
 
162
  setColumnWidth(0, static_cast<int>(width() * 0.3));
 
163
  setColumnWidth(1, static_cast<int>(width() * 0.3));
 
164
  setColumnWidth(2, static_cast<int>(width() * 0.3));
 
165
}
 
166
 
 
167
void CharacterFormEditor::Save(config::Config *config) {
 
168
  if (rowCount() == 0) {
 
169
    return;
 
170
  }
 
171
 
 
172
  config->clear_character_form_rules();
 
173
  for (int row = 0; row < rowCount(); ++row) {
 
174
    if (item(row, 0)->text().isEmpty()) {
 
175
      continue;
 
176
    }
 
177
    const string group = StringToGroup(item(row, 0)->text());
 
178
    config::Config::CharacterForm preedit_form =
 
179
        StringToForm(item(row, 1)->text());
 
180
    config::Config::CharacterForm conversion_form =
 
181
        StringToForm(item(row, 2)->text());
 
182
    config::Config::CharacterFormRule *rule =
 
183
        config->add_character_form_rules();
 
184
    rule->set_group(group);
 
185
    rule->set_preedit_character_form(preedit_form);
 
186
    rule->set_conversion_character_form(conversion_form);
 
187
  }
 
188
}
 
189
}  // gui
 
190
}  // mozc