| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2023~2023 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "customphraseeditor.h" |
| 8 |
#include "customphrasemodel.h" |
| 9 |
#include "editordialog.h" |
| 10 |
#include <QAbstractItemModel> |
| 11 |
#include <QDialog> |
| 12 |
#include <QFutureWatcher> |
| 13 |
#include <QHeaderView> |
| 14 |
#include <QLineEdit> |
| 15 |
#include <QObject> |
| 16 |
#include <QPushButton> |
| 17 |
#include <QRegularExpressionValidator> |
| 18 |
#include <QSpinBox> |
| 19 |
#include <QStyleOptionViewItem> |
| 20 |
#include <QStyledItemDelegate> |
| 21 |
#include <QTextEdit> |
| 22 |
#include <fcitx-utils/i18n.h> |
| 23 |
#include <fcitx-utils/standardpaths.h> |
| 24 |
#include <fcitx-utils/stringutils.h> |
| 25 |
#include <fcitxqtconfiguiwidget.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <qdesktopservices.h> |
| 28 |
#include <qfilesystemwatcher.h> |
| 29 |
#include <qmessagebox.h> |
| 30 |
#include <qnamespace.h> |
| 31 |
|
| 32 |
namespace fcitx { |
| 33 |
|
| 34 |
class OrderDelegate : public QStyledItemDelegate { |
| 35 |
Q_OBJECT |
| 36 |
public: |
| 37 |
OrderDelegate(QObject *parent) : QStyledItemDelegate(parent) {} |
| 38 |
QWidget *createEditor(QWidget *parent, |
| 39 |
const QStyleOptionViewItem & /*option*/, |
| 40 |
const QModelIndex & /*index*/) const override { |
| 41 |
auto *spinBox = new QSpinBox(parent); |
| 42 |
spinBox->setFrame(false); |
| 43 |
spinBox->setMinimum(1); |
| 44 |
spinBox->setMaximum(100); |
| 45 |
return spinBox; |
| 46 |
} |
| 47 |
void setEditorData(QWidget *editor, |
| 48 |
const QModelIndex &index) const override { |
| 49 |
int value = index.model()->data(index, Qt::EditRole).toInt(); |
| 50 |
|
| 51 |
auto *spinBox = static_cast<QSpinBox *>(editor); |
| 52 |
spinBox->setValue(value); |
| 53 |
} |
| 54 |
void setModelData(QWidget *editor, QAbstractItemModel *model, |
| 55 |
const QModelIndex &index) const override { |
| 56 |
auto *spinBox = static_cast<QSpinBox *>(editor); |
| 57 |
spinBox->interpretText(); |
| 58 |
int value = spinBox->value(); |
| 59 |
|
| 60 |
model->setData(index, value, Qt::EditRole); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
class KeyDelegate : public QStyledItemDelegate { |
| 65 |
Q_OBJECT |
| 66 |
public: |
| 67 |
KeyDelegate(QObject *parent) : QStyledItemDelegate(parent) {} |
| 68 |
QWidget *createEditor(QWidget *parent, |
| 69 |
const QStyleOptionViewItem & /*option*/, |
| 70 |
const QModelIndex & /*index*/) const override { |
| 71 |
auto *lineEdit = new QLineEdit(parent); |
| 72 |
auto *validator = new QRegularExpressionValidator(lineEdit); |
| 73 |
|
| 74 |
QRegularExpression reg("[a-zA-Z]+"); |
| 75 |
validator->setRegularExpression(reg); |
| 76 |
lineEdit->setValidator(validator); |
| 77 |
return lineEdit; |
| 78 |
} |
| 79 |
void setEditorData(QWidget *editor, |
| 80 |
const QModelIndex &index) const override { |
| 81 |
auto value = index.model()->data(index, Qt::EditRole).toString(); |
| 82 |
|
| 83 |
auto *lineEdit = static_cast<QLineEdit *>(editor); |
| 84 |
lineEdit->setText(value); |
| 85 |
} |
| 86 |
void setModelData(QWidget *editor, QAbstractItemModel *model, |
| 87 |
const QModelIndex &index) const override { |
| 88 |
QLineEdit *lineEdit = static_cast<QLineEdit *>(editor); |
| 89 |
if (lineEdit->hasAcceptableInput()) { |
| 90 |
model->setData(index, lineEdit->text(), Qt::EditRole); |
| 91 |
} |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
class ValueDelegate : public QStyledItemDelegate { |
| 96 |
Q_OBJECT |
| 97 |
public: |
| 98 |
ValueDelegate(QObject *parent) : QStyledItemDelegate(parent) {} |
| 99 |
QWidget *createEditor(QWidget *parent, |
| 100 |
const QStyleOptionViewItem & /*option*/, |
| 101 |
const QModelIndex & /*index*/) const override { |
| 102 |
auto *textEdit = new QTextEdit(parent); |
| 103 |
textEdit->setAcceptRichText(false); |
| 104 |
return textEdit; |
| 105 |
} |
| 106 |
void setEditorData(QWidget *editor, |
| 107 |
const QModelIndex &index) const override { |
| 108 |
auto value = index.model()->data(index, Qt::EditRole).toString(); |
| 109 |
|
| 110 |
auto *textEdit = static_cast<QTextEdit *>(editor); |
| 111 |
textEdit->setPlainText(value); |
| 112 |
} |
| 113 |
void setModelData(QWidget *editor, QAbstractItemModel *model, |
| 114 |
const QModelIndex &index) const override { |
| 115 |
auto *textEdit = static_cast<QTextEdit *>(editor); |
| 116 |
model->setData(index, textEdit->toPlainText(), Qt::EditRole); |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
CustomPhraseEditor::CustomPhraseEditor(QWidget *parent) |
| 121 |
: FcitxQtConfigUIWidget(parent), model_(new CustomPhraseModel(this)), |
| 122 |
userFile_(QString::fromStdU16String( |
| 123 |
(StandardPaths::global().userDirectory(StandardPathsType::PkgData) / |
| 124 |
customPhraseFileName) |
| 125 |
.u16string())) { |
| 126 |
setupUi(this); |
| 127 |
|
| 128 |
connect(addButton_, &QPushButton::clicked, this, |
| 129 |
&CustomPhraseEditor::addPhrase); |
| 130 |
connect(removeButton_, &QPushButton::clicked, this, |
| 131 |
&CustomPhraseEditor::removePhrase); |
| 132 |
connect(clearButton_, &QPushButton::clicked, this, |
| 133 |
&CustomPhraseEditor::clear); |
| 134 |
connect(externalEditor_, &QPushButton::clicked, this, |
| 135 |
&CustomPhraseEditor::openExternal); |
| 136 |
connect(helpButton_, &QPushButton::clicked, this, [this]() { |
| 137 |
QMessageBox::information( |
| 138 |
this, QString(_("Usage")), |
| 139 |
QString::fromStdString(customPhraseHelpMessage())); |
| 140 |
}); |
| 141 |
connectFileWatcher(); |
| 142 |
|
| 143 |
tableView_->setModel(model_); |
| 144 |
tableView_->horizontalHeader()->setSectionResizeMode( |
| 145 |
CustomPhraseModel::Column_Enable, QHeaderView::ResizeToContents); |
| 146 |
tableView_->horizontalHeader()->setSectionResizeMode( |
| 147 |
CustomPhraseModel::Column_Key, QHeaderView::ResizeToContents); |
| 148 |
tableView_->horizontalHeader()->setSectionResizeMode( |
| 149 |
CustomPhraseModel::Column_Phrase, QHeaderView::Stretch); |
| 150 |
tableView_->horizontalHeader()->setSectionResizeMode( |
| 151 |
CustomPhraseModel::Column_Order, QHeaderView::ResizeToContents); |
| 152 |
tableView_->setItemDelegateForColumn(CustomPhraseModel::Column_Key, |
| 153 |
new KeyDelegate(this)); |
| 154 |
tableView_->setItemDelegateForColumn(CustomPhraseModel::Column_Phrase, |
| 155 |
new ValueDelegate(this)); |
| 156 |
tableView_->setItemDelegateForColumn(CustomPhraseModel::Column_Order, |
| 157 |
new OrderDelegate(this)); |
| 158 |
|
| 159 |
connect(model_, &CustomPhraseModel::needSaveChanged, this, |
| 160 |
&CustomPhraseEditor::changed); |
| 161 |
load(); |
| 162 |
} |
| 163 |
|
| 164 |
void CustomPhraseEditor::load() { model_->load(); } |
| 165 |
|
| 166 |
void CustomPhraseEditor::save() { |
| 167 |
disconnectFileWatcher(); |
| 168 |
QFutureWatcher<bool> *futureWatcher = model_->save(); |
| 169 |
connect(futureWatcher, &QFutureWatcherBase::finished, this, [this]() { |
| 170 |
saveFinished(); |
| 171 |
connectFileWatcher(); |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
QString CustomPhraseEditor::title() { return _("Custom Phrase Editor"); } |
| 176 |
|
| 177 |
bool CustomPhraseEditor::asyncSave() { return true; } |
| 178 |
|
| 179 |
void CustomPhraseEditor::addPhrase() { |
| 180 |
EditorDialog *dialog = new EditorDialog(this); |
| 181 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 182 |
dialog->open(); |
| 183 |
connect(dialog, &QDialog::accepted, this, |
| 184 |
&CustomPhraseEditor::addPhraseAccepted); |
| 185 |
} |
| 186 |
|
| 187 |
void CustomPhraseEditor::addPhraseAccepted() { |
| 188 |
const EditorDialog *dialog = |
| 189 |
qobject_cast<const EditorDialog *>(QObject::sender()); |
| 190 |
|
| 191 |
model_->addItem(dialog->key(), dialog->value(), dialog->order(), true); |
| 192 |
QModelIndex last = model_->index(model_->rowCount() - 1, 0); |
| 193 |
tableView_->setCurrentIndex(last); |
| 194 |
tableView_->scrollTo(last); |
| 195 |
} |
| 196 |
|
| 197 |
void CustomPhraseEditor::removePhrase() { |
| 198 |
if (!tableView_->currentIndex().isValid()) { |
| 199 |
return; |
| 200 |
} |
| 201 |
int row = tableView_->currentIndex().row(); |
| 202 |
model_->deleteItem(row); |
| 203 |
} |
| 204 |
|
| 205 |
void CustomPhraseEditor::clear() { model_->deleteAllItem(); } |
| 206 |
|
| 207 |
void CustomPhraseEditor::openExternal() { |
| 208 |
disconnectFileWatcher(); |
| 209 |
model_->save()->waitForFinished(); |
| 210 |
connectFileWatcher(); |
| 211 |
QDesktopServices::openUrl(QUrl::fromLocalFile(userFile_)); |
| 212 |
} |
| 213 |
|
| 214 |
void CustomPhraseEditor::updated() { |
| 215 |
disconnectFileWatcher(); |
| 216 |
if (QMessageBox::Yes == |
| 217 |
QMessageBox::question( |
| 218 |
this, _("File updated"), |
| 219 |
_("Do you want to reload custom phrase from disk?"))) { |
| 220 |
load(); |
| 221 |
// Trigger a reload on fcitx |
| 222 |
saveSubConfig("fcitx://config/addon/pinyin/customphrase"); |
| 223 |
} else { |
| 224 |
Q_EMIT changed(true); |
| 225 |
} |
| 226 |
connectFileWatcher(); |
| 227 |
} |
| 228 |
|
| 229 |
void CustomPhraseEditor::disconnectFileWatcher() { |
| 230 |
disconnect(&watcher_, &QFileSystemWatcher::fileChanged, this, |
| 231 |
&CustomPhraseEditor::updated); |
| 232 |
} |
| 233 |
void CustomPhraseEditor::connectFileWatcher() { |
| 234 |
watcher_.removePath(userFile_); |
| 235 |
watcher_.addPath(userFile_); |
| 236 |
connect(&watcher_, &QFileSystemWatcher::fileChanged, this, |
| 237 |
&CustomPhraseEditor::updated, Qt::UniqueConnection); |
| 238 |
} |
| 239 |
|
| 240 |
} // namespace fcitx |
| 241 |
|
| 242 |
#include "customphraseeditor.moc" |