| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "editor.h" |
| 8 |
#include "batchdialog.h" |
| 9 |
#include "editordialog.h" |
| 10 |
#include "filelistmodel.h" |
| 11 |
#include "model.h" |
| 12 |
#include <QCloseEvent> |
| 13 |
#include <QDebug> |
| 14 |
#include <QFileDialog> |
| 15 |
#include <QInputDialog> |
| 16 |
#include <QMenu> |
| 17 |
#include <QMessageBox> |
| 18 |
#include <QPushButton> |
| 19 |
#include <QtConcurrentRun> |
| 20 |
#include <fcitx-utils/i18n.h> |
| 21 |
#include <fcitx-utils/standardpaths.h> |
| 22 |
#include <filesystem> |
| 23 |
|
| 24 |
namespace fcitx { |
| 25 |
|
| 26 |
ListEditor::ListEditor(QWidget *parent) |
| 27 |
: FcitxQtConfigUIWidget(parent), model_(new QuickPhraseModel(this)), |
| 28 |
fileListModel_(new FileListModel(this)) { |
| 29 |
setupUi(this); |
| 30 |
|
| 31 |
macroTableView->setModel(model_); |
| 32 |
fileListComboBox->setModel(fileListModel_); |
| 33 |
|
| 34 |
operationMenu_ = new QMenu(this); |
| 35 |
operationMenu_->addAction(_("Add File"), this, |
| 36 |
&ListEditor::addFileTriggered); |
| 37 |
operationMenu_->addAction(_("Remove File"), this, |
| 38 |
&ListEditor::removeFileTriggered); |
| 39 |
operationMenu_->addAction(_("Refresh List"), this, |
| 40 |
&ListEditor::refreshListTriggered); |
| 41 |
operationButton->setMenu(operationMenu_); |
| 42 |
|
| 43 |
loadFileList(); |
| 44 |
itemFocusChanged(); |
| 45 |
|
| 46 |
connect(addButton, &QPushButton::clicked, this, &ListEditor::addWord); |
| 47 |
connect(batchEditButton, &QPushButton::clicked, this, |
| 48 |
&ListEditor::batchEditWord); |
| 49 |
connect(deleteButton, &QPushButton::clicked, this, &ListEditor::deleteWord); |
| 50 |
connect(clearButton, &QPushButton::clicked, this, |
| 51 |
&ListEditor::deleteAllWord); |
| 52 |
connect(importButton, &QPushButton::clicked, this, &ListEditor::importData); |
| 53 |
connect(exportButton, &QPushButton::clicked, this, &ListEditor::exportData); |
| 54 |
|
| 55 |
connect(fileListComboBox, qOverload<int>(&QComboBox::activated), this, |
| 56 |
&ListEditor::changeFile); |
| 57 |
|
| 58 |
connect(macroTableView->selectionModel(), |
| 59 |
&QItemSelectionModel::selectionChanged, this, |
| 60 |
&ListEditor::itemFocusChanged); |
| 61 |
connect(model_, &QuickPhraseModel::needSaveChanged, this, |
| 62 |
&ListEditor::changed); |
| 63 |
} |
| 64 |
|
| 65 |
void ListEditor::load() { |
| 66 |
lastFile_ = currentFile(); |
| 67 |
model_->load(currentFile(), false); |
| 68 |
} |
| 69 |
|
| 70 |
void ListEditor::load(const QString &file) { model_->load(file, true); } |
| 71 |
|
| 72 |
void ListEditor::save(const QString &file) { model_->save(file); } |
| 73 |
|
| 74 |
void ListEditor::save() { |
| 75 |
// QFutureWatcher< bool >* futureWatcher = |
| 76 |
// m_model->save("data/QuickPhrase.mb"); |
| 77 |
QFutureWatcher<bool> *futureWatcher = model_->save(currentFile()); |
| 78 |
connect(futureWatcher, &QFutureWatcherBase::finished, this, |
| 79 |
&ListEditor::saveFinished); |
| 80 |
} |
| 81 |
|
| 82 |
bool ListEditor::asyncSave() { return true; } |
| 83 |
|
| 84 |
void ListEditor::changeFile(int) { |
| 85 |
if (model_->needSave()) { |
| 86 |
int ret = QMessageBox::question( |
| 87 |
this, _("Save Changes"), |
| 88 |
_("The content has changed.\n" |
| 89 |
"Do you want to save the changes or discard them?"), |
| 90 |
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); |
| 91 |
if (ret == QMessageBox::Save) { |
| 92 |
// save(fileListComboBox->itemText(lastFileIndex)); |
| 93 |
save(lastFile_); |
| 94 |
} else if (ret == QMessageBox::Cancel) { |
| 95 |
fileListComboBox->setCurrentIndex( |
| 96 |
fileListModel_->findFile(lastFile_)); |
| 97 |
return; |
| 98 |
} |
| 99 |
} |
| 100 |
load(); |
| 101 |
} |
| 102 |
|
| 103 |
QString ListEditor::title() { return _("Quick Phrase Editor"); } |
| 104 |
|
| 105 |
void ListEditor::itemFocusChanged() { |
| 106 |
deleteButton->setEnabled(macroTableView->currentIndex().isValid()); |
| 107 |
} |
| 108 |
|
| 109 |
void ListEditor::deleteWord() { |
| 110 |
if (!macroTableView->currentIndex().isValid()) |
| 111 |
return; |
| 112 |
int row = macroTableView->currentIndex().row(); |
| 113 |
model_->deleteItem(row); |
| 114 |
} |
| 115 |
|
| 116 |
void ListEditor::deleteAllWord() { model_->deleteAllItem(); } |
| 117 |
|
| 118 |
void ListEditor::addWord() { |
| 119 |
EditorDialog *dialog = new EditorDialog(this); |
| 120 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 121 |
dialog->open(); |
| 122 |
connect(dialog, &QDialog::accepted, this, &ListEditor::addWordAccepted); |
| 123 |
} |
| 124 |
|
| 125 |
void ListEditor::batchEditWord() { |
| 126 |
BatchDialog *dialog = new BatchDialog(this); |
| 127 |
QString text; |
| 128 |
QTextStream stream(&text); |
| 129 |
model_->saveDataToStream(stream); |
| 130 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 131 |
dialog->setText(text); |
| 132 |
dialog->open(); |
| 133 |
connect(dialog, &QDialog::accepted, this, &ListEditor::batchEditAccepted); |
| 134 |
} |
| 135 |
|
| 136 |
void ListEditor::addWordAccepted() { |
| 137 |
const EditorDialog *dialog = |
| 138 |
qobject_cast<const EditorDialog *>(QObject::sender()); |
| 139 |
|
| 140 |
model_->addItem(dialog->key(), dialog->value()); |
| 141 |
QModelIndex last = model_->index(model_->rowCount() - 1, 0); |
| 142 |
macroTableView->setCurrentIndex(last); |
| 143 |
macroTableView->scrollTo(last); |
| 144 |
} |
| 145 |
|
| 146 |
void ListEditor::batchEditAccepted() { |
| 147 |
const BatchDialog *dialog = |
| 148 |
qobject_cast<const BatchDialog *>(QObject::sender()); |
| 149 |
|
| 150 |
QString s = dialog->text(); |
| 151 |
QTextStream stream(&s); |
| 152 |
|
| 153 |
model_->loadData(stream); |
| 154 |
QModelIndex last = model_->index(model_->rowCount() - 1, 0); |
| 155 |
macroTableView->setCurrentIndex(last); |
| 156 |
macroTableView->scrollTo(last); |
| 157 |
} |
| 158 |
|
| 159 |
void ListEditor::importData() { |
| 160 |
QFileDialog *dialog = new QFileDialog(this); |
| 161 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 162 |
dialog->setFileMode(QFileDialog::ExistingFile); |
| 163 |
dialog->setAcceptMode(QFileDialog::AcceptOpen); |
| 164 |
dialog->open(); |
| 165 |
connect(dialog, &QDialog::accepted, this, &ListEditor::importFileSelected); |
| 166 |
} |
| 167 |
|
| 168 |
void ListEditor::exportData() { |
| 169 |
QFileDialog *dialog = new QFileDialog(this); |
| 170 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 171 |
dialog->setAcceptMode(QFileDialog::AcceptSave); |
| 172 |
dialog->open(); |
| 173 |
connect(dialog, &QDialog::accepted, this, &ListEditor::exportFileSelected); |
| 174 |
} |
| 175 |
|
| 176 |
void ListEditor::importFileSelected() { |
| 177 |
const QFileDialog *dialog = |
| 178 |
qobject_cast<const QFileDialog *>(QObject::sender()); |
| 179 |
if (dialog->selectedFiles().length() <= 0) |
| 180 |
return; |
| 181 |
QString file = dialog->selectedFiles()[0]; |
| 182 |
load(file); |
| 183 |
} |
| 184 |
|
| 185 |
void ListEditor::exportFileSelected() { |
| 186 |
const QFileDialog *dialog = |
| 187 |
qobject_cast<const QFileDialog *>(QObject::sender()); |
| 188 |
if (dialog->selectedFiles().length() <= 0) |
| 189 |
return; |
| 190 |
QString file = dialog->selectedFiles()[0]; |
| 191 |
save(file); |
| 192 |
} |
| 193 |
|
| 194 |
void ListEditor::loadFileList() { |
| 195 |
int row = fileListComboBox->currentIndex(); |
| 196 |
int col = fileListComboBox->modelColumn(); |
| 197 |
QString lastFileName = |
| 198 |
fileListModel_->data(fileListModel_->index(row, col), Qt::UserRole) |
| 199 |
.toString(); |
| 200 |
fileListModel_->loadFileList(); |
| 201 |
fileListComboBox->setCurrentIndex(fileListModel_->findFile(lastFileName)); |
| 202 |
load(); |
| 203 |
} |
| 204 |
|
| 205 |
QString ListEditor::currentFile() { |
| 206 |
int row = fileListComboBox->currentIndex(); |
| 207 |
int col = fileListComboBox->modelColumn(); |
| 208 |
return fileListModel_->data(fileListModel_->index(row, col), Qt::UserRole) |
| 209 |
.toString(); |
| 210 |
} |
| 211 |
|
| 212 |
QString ListEditor::currentName() { |
| 213 |
int row = fileListComboBox->currentIndex(); |
| 214 |
int col = fileListComboBox->modelColumn(); |
| 215 |
return fileListModel_ |
| 216 |
->data(fileListModel_->index(row, col), Qt::DisplayRole) |
| 217 |
.toString(); |
| 218 |
} |
| 219 |
|
| 220 |
void ListEditor::addFileTriggered() { |
| 221 |
bool ok; |
| 222 |
QString filename = QInputDialog::getText( |
| 223 |
this, _("Create new file"), _("Please input a filename for newfile"), |
| 224 |
QLineEdit::Normal, "newfile", &ok); |
| 225 |
|
| 226 |
if (filename.contains('/')) { |
| 227 |
QMessageBox::warning(this, _("Invalid filename"), |
| 228 |
_("File name should not contain '/'.")); |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
filename.append(".mb"); |
| 233 |
if (!StandardPaths::global().safeSave( |
| 234 |
StandardPathsType::PkgData, |
| 235 |
std::filesystem::path(QUICK_PHRASE_CONFIG_DIR) / |
| 236 |
filename.toStdString(), |
| 237 |
[](int) { return true; })) { |
| 238 |
QMessageBox::warning( |
| 239 |
this, _("File Operation Failed"), |
| 240 |
QString(_("Cannot create file %1.")).arg(filename)); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
fileListModel_->loadFileList(); |
| 245 |
fileListComboBox->setCurrentIndex(fileListModel_->findFile( |
| 246 |
filename.prepend(QUICK_PHRASE_CONFIG_DIR "/"))); |
| 247 |
load(); |
| 248 |
} |
| 249 |
|
| 250 |
void ListEditor::refreshListTriggered() { loadFileList(); } |
| 251 |
|
| 252 |
void ListEditor::removeFileTriggered() { |
| 253 |
QString filename = currentFile(); |
| 254 |
QString curName = currentName(); |
| 255 |
auto fullname = |
| 256 |
StandardPaths::global().userDirectory(StandardPathsType::PkgData) / |
| 257 |
filename.toStdString(); |
| 258 |
QFile f(QString::fromStdString(fullname.string())); |
| 259 |
if (!f.exists()) { |
| 260 |
int ret = QMessageBox::question( |
| 261 |
this, _("Cannot remove system file"), |
| 262 |
QString(_("%1 is a system file, do you want to delete all phrases " |
| 263 |
"instead?")) |
| 264 |
.arg(curName), |
| 265 |
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); |
| 266 |
if (ret == QMessageBox::Yes) { |
| 267 |
deleteAllWord(); |
| 268 |
} |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
int ret = QMessageBox::question( |
| 273 |
this, _("Confirm deletion"), |
| 274 |
QString(_("Are you sure to delete %1?")).arg(curName), |
| 275 |
QMessageBox::Ok | QMessageBox::Cancel); |
| 276 |
|
| 277 |
if (ret == QMessageBox::Ok) { |
| 278 |
bool ok = f.remove(); |
| 279 |
if (!ok) { |
| 280 |
QMessageBox::warning( |
| 281 |
this, _("File Operation Failed"), |
| 282 |
QString(_("Error while deleting %1.")).arg(curName)); |
| 283 |
} |
| 284 |
} |
| 285 |
loadFileList(); |
| 286 |
load(); |
| 287 |
} |
| 288 |
} // namespace fcitx |