9#include <QtWidgets/QMessageBox>
10#include <QtWidgets/QProgressDialog>
19Qt::ItemFlags CheckableListModel::flags(
const QModelIndex &index)
const
22 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
26 : QDialog(w), m_model(
this), m_dataModel(dataModel)
29 connect(m_ui.runButton, &QAbstractButton::clicked,
30 this, &BatchTranslationDialog::startTranslation);
31 connect(m_ui.moveUpButton, &QAbstractButton::clicked,
32 this, &BatchTranslationDialog::movePhraseBookUp);
33 connect(m_ui.moveDownButton, &QAbstractButton::clicked,
34 this, &BatchTranslationDialog::movePhraseBookDown);
36 m_ui.phrasebookList->setModel(&m_model);
37 m_ui.phrasebookList->setSelectionBehavior(QAbstractItemView::SelectItems);
38 m_ui.phrasebookList->setSelectionMode(QAbstractItemView::SingleSelection);
44 QString fn = QFileInfo(m_dataModel->srcFileName(modelIndex)).baseName();
45 setWindowTitle(tr(
"Batch Translation of '%1' - Qt Linguist").arg(fn));
47 m_model.insertColumn(0);
48 m_phrasebooks = phrasebooks;
49 m_modelIndex = modelIndex;
50 int count = phrasebooks.size();
51 m_model.insertRows(0, count);
52 for (
int i = 0; i < count; ++i) {
53 QModelIndex idx(m_model.index(i, 0));
54 m_model.setData(idx, phrasebooks[i]->friendlyPhraseBookName());
56 if (phrasebooks[i]->language() != QLocale::C
57 && m_dataModel->language(m_modelIndex) != QLocale::C) {
58 if (phrasebooks[i]->language() != m_dataModel->language(m_modelIndex))
61 sortOrder = (phrasebooks[i]->territory()
62 == m_dataModel
->model(m_modelIndex
)->territory()) ? 0 : 1;
66 m_model.setData(idx, sortOrder == 3 ? Qt::Unchecked : Qt::Checked, Qt::CheckStateRole);
67 m_model.setData(idx, sortOrder, Qt::UserRole + 1);
68 m_model.setData(idx, i, Qt::UserRole);
70 m_model.setSortRole(Qt::UserRole + 1);
76 int translatedcount = 0;
77 QCursor oldCursor = cursor();
78 setCursor(Qt::BusyCursor);
81 QProgressDialog *dlgProgress;
82 dlgProgress =
new QProgressDialog(tr(
"Searching, please wait..."), tr(
"&Cancel"), 0, messageCount,
this);
86 const bool translateTranslated = m_ui.ckTranslateTranslated->isChecked();
87 const bool translateFinished = m_ui.ckTranslateFinished->isChecked();
89 auto translate = [translateTranslated, translateFinished, &translatedcount, &msgidx,
90 dlgProgress,
this](
auto it) {
91 for (; it.isValid(); ++it) {
93 if (!m
->isObsolete() && (translateTranslated || m->translation().isEmpty())
97 for (
int b = 0; b < m_model.rowCount(); ++b) {
98 QModelIndex idx(m_model.index(b, 0));
99 QVariant checkState = m_model.data(idx, Qt::CheckStateRole);
100 if (checkState == Qt::Checked) {
101 PhraseBook *pb = m_phrasebooks[m_model.data(idx, Qt::UserRole).toInt()];
102 const auto phrases = pb->phrases();
103 for (
const Phrase *ph : phrases) {
104 if (ph->source() == m->text()) {
105 m_dataModel->setTranslation(it, ph->target());
106 m_dataModel->setFinished(it, m_ui.ckMarkFinished->isChecked());
118 dlgProgress->setValue(msgidx);
119 qApp->processEvents();
120 if (dlgProgress->wasCanceled())
130 setCursor(oldCursor);
132 QMessageBox::information(
this, tr(
"Linguist batch translator"),
133 tr(
"Batch translated %n entries",
"", translatedcount), QMessageBox::Ok);
138 QModelIndexList indexes = m_ui.phrasebookList->selectionModel()->selectedIndexes();
139 if (indexes.size() <= 0)
return;
141 QModelIndex sel = indexes[0];
144 QModelIndex other = m_model.index(row - 1, 0);
145 QMap<
int, QVariant> seldata = m_model.itemData(sel);
146 m_model.setItemData(sel, m_model.itemData(other));
147 m_model.setItemData(other, seldata);
148 m_ui.phrasebookList->selectionModel()->setCurrentIndex(other, QItemSelectionModel::ClearAndSelect);
154 QModelIndexList indexes = m_ui.phrasebookList->selectionModel()->selectedIndexes();
155 if (indexes.size() <= 0)
return;
157 QModelIndex sel = indexes[0];
159 if (row < m_model.rowCount() - 1) {
160 QModelIndex other = m_model.index(row + 1, 0);
161 QMap<
int, QVariant> seldata = m_model.itemData(sel);
162 m_model.setItemData(sel, m_model.itemData(other));
163 m_model.setItemData(other, seldata);
164 m_ui.phrasebookList->selectionModel()->setCurrentIndex(other, QItemSelectionModel::ClearAndSelect);
void setPhraseBooks(const QList< PhraseBook * > &phrasebooks, int modelIndex)
MultiDataModelIterator(TranslationType type, MultiDataModel *model=0, int modelNo=-1, int groupNo=0, int messageNo=0)