Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
batchtranslationdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5#include "phrase.h"
6#include "messagemodel.h"
7
8#include <QtCore/QMap>
9#include <QtWidgets/QMessageBox>
10#include <QtWidgets/QProgressDialog>
11
13
18
19Qt::ItemFlags CheckableListModel::flags(const QModelIndex &index) const
20{
21 Q_UNUSED(index);
22 return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
23}
24
25BatchTranslationDialog::BatchTranslationDialog(MultiDataModel *dataModel, QWidget *w)
26 : QDialog(w), m_model(this), m_dataModel(dataModel)
27{
28 m_ui.setupUi(this);
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);
35
36 m_ui.phrasebookList->setModel(&m_model);
37 m_ui.phrasebookList->setSelectionBehavior(QAbstractItemView::SelectItems);
38 m_ui.phrasebookList->setSelectionMode(QAbstractItemView::SingleSelection);
39}
40
41
42void BatchTranslationDialog::setPhraseBooks(const QList<PhraseBook *> &phrasebooks, int modelIndex)
43{
44 QString fn = QFileInfo(m_dataModel->srcFileName(modelIndex)).baseName();
45 setWindowTitle(tr("Batch Translation of '%1' - Qt Linguist").arg(fn));
46 m_model.clear();
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());
55 int sortOrder;
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))
59 sortOrder = 3;
60 else
61 sortOrder = (phrasebooks[i]->territory()
62 == m_dataModel->model(m_modelIndex)->territory()) ? 0 : 1;
63 } else {
64 sortOrder = 2;
65 }
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);
69 }
70 m_model.setSortRole(Qt::UserRole + 1);
71 m_model.sort(0);
72}
73
74void BatchTranslationDialog::startTranslation()
75{
76 int translatedcount = 0;
77 QCursor oldCursor = cursor();
78 setCursor(Qt::BusyCursor);
79 int messageCount = m_dataModel->messageCount();
80
81 QProgressDialog *dlgProgress;
82 dlgProgress = new QProgressDialog(tr("Searching, please wait..."), tr("&Cancel"), 0, messageCount, this);
83 dlgProgress->show();
84
85 int msgidx = 0;
86 const bool translateTranslated = m_ui.ckTranslateTranslated->isChecked();
87 const bool translateFinished = m_ui.ckTranslateFinished->isChecked();
88
89 auto translate = [translateTranslated, translateFinished, &translatedcount, &msgidx,
90 dlgProgress, this](auto it) {
91 for (; it.isValid(); ++it) {
92 if (MessageItem *m = it.current()) {
93 if (!m->isObsolete() && (translateTranslated || m->translation().isEmpty())
94 && (translateFinished || !m->isFinished())) {
95
96 // Go through them in the order the user specified in the phrasebookList
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());
107 ++translatedcount;
108 goto done; // break 2;
109 }
110 }
111 }
112 }
113 }
114 }
115 done:
116 ++msgidx;
117 if (!(msgidx & 15))
118 dlgProgress->setValue(msgidx);
119 qApp->processEvents();
120 if (dlgProgress->wasCanceled())
121 break;
122 }
123 };
124
125 translate(MultiDataModelIterator(IDBASED, m_dataModel, m_modelIndex));
126 translate(MultiDataModelIterator(TEXTBASED, m_dataModel, m_modelIndex));
127
128 dlgProgress->hide();
129
130 setCursor(oldCursor);
131 emit finished();
132 QMessageBox::information(this, tr("Linguist batch translator"),
133 tr("Batch translated %n entries", "", translatedcount), QMessageBox::Ok);
134}
135
136void BatchTranslationDialog::movePhraseBookUp()
137{
138 QModelIndexList indexes = m_ui.phrasebookList->selectionModel()->selectedIndexes();
139 if (indexes.size() <= 0) return;
140
141 QModelIndex sel = indexes[0];
142 int row = sel.row();
143 if (row > 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);
149 }
150}
151
152void BatchTranslationDialog::movePhraseBookDown()
153{
154 QModelIndexList indexes = m_ui.phrasebookList->selectionModel()->selectedIndexes();
155 if (indexes.size() <= 0) return;
156
157 QModelIndex sel = indexes[0];
158 int row = sel.row();
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);
165 }
166}
167
168QT_END_NAMESPACE
void setPhraseBooks(const QList< PhraseBook * > &phrasebooks, int modelIndex)
bool isFinished() const
bool isObsolete() const
MultiDataModelIterator(TranslationType type, MultiDataModel *model=0, int modelNo=-1, int groupNo=0, int messageNo=0)
DataModel * model(int i)
int messageCount() const
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:105
@ IDBASED
@ TEXTBASED