| 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 <QApplication> |
| 21 |
|
| 22 |
#include "model.h" |
| 23 |
#include "common.h" |
| 24 |
#include "editor.h" |
| 25 |
|
| 26 |
namespace fcitx_unikey { |
| 27 |
|
| 28 |
typedef QPair<QString, QString> ItemType; |
| 29 |
|
| 30 |
MacroModel::MacroModel(QObject* parent): QAbstractTableModel(parent) |
| 31 |
,m_needSave(false) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
MacroModel::~MacroModel() |
| 36 |
{ |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
QVariant MacroModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 41 |
{ |
| 42 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 43 |
if (section == 0) |
| 44 |
return _("Macro"); |
| 45 |
else if (section == 1) |
| 46 |
return _("Word"); |
| 47 |
} |
| 48 |
return QVariant(); |
| 49 |
} |
| 50 |
|
| 51 |
int MacroModel::rowCount(const QModelIndex& parent) const |
| 52 |
{ |
| 53 |
return m_list.count(); |
| 54 |
} |
| 55 |
|
| 56 |
int MacroModel::columnCount(const QModelIndex& parent) const |
| 57 |
{ |
| 58 |
return 2; |
| 59 |
} |
| 60 |
|
| 61 |
QVariant MacroModel::data(const QModelIndex& index, int role) const |
| 62 |
{ |
| 63 |
do { |
| 64 |
if (role == Qt::DisplayRole && index.row() < m_list.count()) { |
| 65 |
if (index.column() == 0) { |
| 66 |
return m_list[index.row()].first; |
| 67 |
} else if (index.column() == 1) { |
| 68 |
return m_list[index.row()].second; |
| 69 |
} |
| 70 |
} |
| 71 |
} while(0); |
| 72 |
return QVariant(); |
| 73 |
} |
| 74 |
|
| 75 |
void MacroModel::addItem(const QString& macro, const QString& word) |
| 76 |
{ |
| 77 |
if (m_keyset.contains(macro)) |
| 78 |
return; |
| 79 |
beginInsertRows(QModelIndex(), m_list.size(), m_list.size()); |
| 80 |
m_list.append(QPair<QString, QString>(macro, word)); |
| 81 |
m_keyset.insert(macro); |
| 82 |
endInsertRows(); |
| 83 |
setNeedSave(true); |
| 84 |
} |
| 85 |
|
| 86 |
void MacroModel::deleteItem(int row) |
| 87 |
{ |
| 88 |
if (row >= m_list.count()) |
| 89 |
return; |
| 90 |
QPair<QString, QString> item = m_list.at(row); |
| 91 |
QString key = item.first; |
| 92 |
beginRemoveRows(QModelIndex(), row, row); |
| 93 |
m_list.removeAt(row); |
| 94 |
m_keyset.remove(key); |
| 95 |
endRemoveRows(); |
| 96 |
setNeedSave(true); |
| 97 |
} |
| 98 |
|
| 99 |
void MacroModel::deleteAllItem() |
| 100 |
{ |
| 101 |
if (m_list.count()) |
| 102 |
setNeedSave(true); |
| 103 |
beginResetModel(); |
| 104 |
m_list.clear(); |
| 105 |
m_keyset.clear(); |
| 106 |
endResetModel(); |
| 107 |
} |
| 108 |
|
| 109 |
void MacroModel::setNeedSave(bool needSave) |
| 110 |
{ |
| 111 |
if (m_needSave != needSave) { |
| 112 |
m_needSave = needSave; |
| 113 |
emit needSaveChanged(m_needSave); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
bool MacroModel::needSave() |
| 118 |
{ |
| 119 |
return m_needSave; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
void MacroModel::load(CMacroTable* table) |
| 124 |
{ |
| 125 |
beginResetModel(); |
| 126 |
m_list.clear(); |
| 127 |
m_keyset.clear(); |
| 128 |
for (int i = 0; i < table->getCount(); i++) { |
| 129 |
QString key = MacroEditor::getData(table, i, true); |
| 130 |
QString value = MacroEditor::getData(table, i, false); |
| 131 |
m_list.append(QPair<QString, QString>(key, value)); |
| 132 |
m_keyset.insert(key); |
| 133 |
} |
| 134 |
endResetModel(); |
| 135 |
} |
| 136 |
|
| 137 |
void MacroModel::save(CMacroTable* m_table) |
| 138 |
{ |
| 139 |
m_table->resetContent(); |
| 140 |
foreach(const ItemType& item, m_list) { |
| 141 |
m_table->addItem(item.first.toUtf8().data(), item.second.toUtf8().data(), CONV_CHARSET_XUTF8); |
| 142 |
} |
| 143 |
setNeedSave(false); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
} |