1 |
#include "wmformatpage.h" |
2 |
#include "ui_wmformatpage.h" |
3 |
|
4 |
WmFormatPage::WmFormatPage(QWidget *parent) : |
5 |
QWizardPage(parent), |
6 |
ui(new Ui::WmFormatPage) |
7 |
{ |
8 |
ui->setupUi(this); |
9 |
|
10 |
this->setTitle("Pages Formatting"); |
11 |
this->setSubTitle("Create and format the pages here.\nNote that Oni doesn't support partial words formatting so the whole line will be formatted."); |
12 |
setupComboBoxTextSize(); |
13 |
|
14 |
ui->cbTextSize->setCurrentText("12"); |
15 |
|
16 |
// Oni only has Tahoma as default, the user however can type any additional font which he has imported into Oni |
17 |
ui->fcbFont->clear(); |
18 |
ui->fcbFont->addItem("Tahoma"); |
19 |
} |
20 |
|
21 |
void WmFormatPage::initializePage() |
22 |
{ |
23 |
// Put here code that is dependent from previous pages |
24 |
|
25 |
// Clean old pages |
26 |
this->currentPages.clear(); |
27 |
ui->twPages->clear(); |
28 |
|
29 |
addPage(); |
30 |
|
31 |
setDefaultOniFont(0); |
32 |
|
33 |
// If the type is diary page we disable the pages adding and removing |
34 |
// To add extra pages to diary is necessary to create additional files |
35 |
switch(static_cast<WINDOW_TYPE>(field("cbWindowType").toInt())){ |
36 |
case WINDOW_TYPE::DIARY: |
37 |
case WINDOW_TYPE::HELP: |
38 |
case WINDOW_TYPE::WEAPON: |
39 |
case WINDOW_TYPE::ITEM: |
40 |
ui->pbAddPageAfter->setEnabled(false); |
41 |
ui->pbAddPageBefore->setEnabled(false); |
42 |
ui->pbDeleteCurrentPage->setEnabled(false); |
43 |
break; |
44 |
case WINDOW_TYPE::OBJECTIVE: |
45 |
case WINDOW_TYPE::TEXT_CONSOLE: |
46 |
ui->pbAddPageAfter->setEnabled(true); |
47 |
ui->pbAddPageBefore->setEnabled(true); |
48 |
ui->pbDeleteCurrentPage->setEnabled(true); |
49 |
break; |
50 |
case WINDOW_TYPE::ENUM_END: |
51 |
UtilVago::showAndLogErrorPopUp("An error ocurred: WmFormatPage::initializePage invalid WINDOW_TYPE"); |
52 |
break; |
53 |
} |
54 |
|
55 |
windowsIsInitialized = true; |
56 |
} |
57 |
|
58 |
WmFormatPage::~WmFormatPage() |
59 |
{ |
60 |
delete ui; |
61 |
} |
62 |
|
63 |
bool WmFormatPage::validatePage(){ |
64 |
|
65 |
for(const std::shared_ptr<WmPage> ¤tPage : this->currentPages){ |
66 |
|
67 |
if( |
68 |
currentPage->getMainText().toPlainText().trimmed() == "" && |
69 |
currentPage->getFooterText().toPlainText().trimmed() == "" |
70 |
) |
71 |
{ |
72 |
Util::Dialogs::showError("You need to fill at least one text section in all pages!"); |
73 |
return false; |
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
// If we can add more than one page, ask the user if he already added them all |
79 |
if(ui->pbAddPageAfter->isEnabled()){ |
80 |
return Util::Dialogs::showQuestion(this, "Have you added all the window pages?"); |
81 |
} |
82 |
|
83 |
return true; |
84 |
|
85 |
} |
86 |
|
87 |
void WmFormatPage::addPage(bool afterCurrentPage){ |
88 |
|
89 |
int indexForInsertion = 0; |
90 |
|
91 |
if(ui->twPages->currentWidget() != nullptr){ |
92 |
if(afterCurrentPage){ |
93 |
indexForInsertion = ui->twPages->currentIndex() + 1; |
94 |
} |
95 |
else{ |
96 |
indexForInsertion = ui->twPages->currentIndex(); |
97 |
} |
98 |
} |
99 |
|
100 |
ui->twPages->insertTab(indexForInsertion, new QWidget(ui->twPages), "Page " + QString::number(indexForInsertion+1)); |
101 |
currentPages.insert(indexForInsertion, std::make_shared<WmPage>(ui->twPages->widget(indexForInsertion))); |
102 |
// Update all other tab texts |
103 |
updateTabNames(indexForInsertion+1); |
104 |
|
105 |
// Set the tab added as current tab |
106 |
ui->twPages->setCurrentIndex(indexForInsertion); |
107 |
|
108 |
// Set the default font |
109 |
setDefaultOniFont(indexForInsertion); |
110 |
// Make the current selected font the default for new tab |
111 |
setCurrentFormatting(indexForInsertion); |
112 |
|
113 |
switch(static_cast<WINDOW_TYPE>(field("cbWindowType").toInt())){ |
114 |
case WINDOW_TYPE::HELP: |
115 |
case WINDOW_TYPE::TEXT_CONSOLE: |
116 |
// these types of page doesn't use the footer |
117 |
this->currentPages[indexForInsertion]->hideFooterText(); |
118 |
break; |
119 |
case WINDOW_TYPE::DIARY: |
120 |
case WINDOW_TYPE::ITEM: |
121 |
case WINDOW_TYPE::OBJECTIVE: |
122 |
case WINDOW_TYPE::WEAPON: |
123 |
this->currentPages[indexForInsertion]->showFooterText(); |
124 |
break; |
125 |
case WINDOW_TYPE::ENUM_END: |
126 |
UtilVago::showAndLogErrorPopUp("An error ocurred: WmFormatPage::addPage invalid WINDOW_TYPE"); |
127 |
break; |
128 |
} |
129 |
|
130 |
} |
131 |
|
132 |
void WmFormatPage::setupComboBoxTextSize(){ |
133 |
|
134 |
// Setup text size combobox |
135 |
for(int i=8; i<=12; i++){ |
136 |
ui->cbTextSize->addItem(QString::number(i)); |
137 |
} |
138 |
|
139 |
for(int i=14; i<=28; i+=2){ |
140 |
ui->cbTextSize->addItem(QString::number(i)); |
141 |
} |
142 |
|
143 |
ui->cbTextSize->addItem(QString::number(36)); |
144 |
|
145 |
} |
146 |
|
147 |
void WmFormatPage::on_tbBold_clicked() |
148 |
{ |
149 |
formatText(FormatType::BOLD); |
150 |
} |
151 |
|
152 |
void WmFormatPage::on_tbItalic_clicked() |
153 |
{ |
154 |
formatText(FormatType::ITALIC); |
155 |
} |
156 |
|
157 |
void WmFormatPage::on_tbFontColor_clicked() |
158 |
{ |
159 |
formatText(FormatType::COLOR); |
160 |
} |
161 |
|
162 |
void WmFormatPage::on_fcbFont_currentFontChanged(const QFont&) |
163 |
{ |
164 |
if(windowsIsInitialized){ |
165 |
formatText(FormatType::FONT_TYPE); |
166 |
} |
167 |
} |
168 |
|
169 |
void WmFormatPage::on_cbTextSize_currentTextChanged(const QString&) |
170 |
{ |
171 |
if(windowsIsInitialized){ |
172 |
formatText(FormatType::FONT_SIZE); |
173 |
} |
174 |
} |
175 |
|
176 |
void WmFormatPage::formatText(FormatType desiredFormatType){ |
177 |
|
178 |
int tabIndex = ui->twPages->currentIndex(); |
179 |
|
180 |
QVector<QTextEdit*> textEdits; |
181 |
|
182 |
if(currentPages[tabIndex]->getMainText().textCursor().hasSelection()){ |
183 |
textEdits << ¤tPages[tabIndex]->getMainText(); |
184 |
} |
185 |
if(currentPages[tabIndex]->getFooterText().textCursor().hasSelection()){ |
186 |
textEdits << ¤tPages[tabIndex]->getFooterText(); |
187 |
} |
188 |
|
189 |
if(textEdits.size() == 0){ |
190 |
Util::Dialogs::showInfo("Select some text first."); |
191 |
return; |
192 |
} |
193 |
|
194 |
for(QTextEdit* const currentTextEdit : textEdits){ |
195 |
QTextCursor currentCursor = currentTextEdit->textCursor(); |
196 |
|
197 |
// Select entire row(s) (oni only supports formatting the entire row) |
198 |
int selectionStart = currentCursor.selectionStart(); |
199 |
int selectionEnd = currentCursor.selectionEnd(); |
200 |
int initialRowNumber = 0; |
201 |
int finalRowNumber = 0; |
202 |
|
203 |
// If we have no selection skip |
204 |
if(selectionStart == selectionEnd){ |
205 |
continue; |
206 |
} |
207 |
|
208 |
currentCursor.setPosition(selectionStart); |
209 |
initialRowNumber = currentCursor.blockNumber(); |
210 |
currentCursor.setPosition(selectionEnd); |
211 |
finalRowNumber = currentCursor.blockNumber(); |
212 |
|
213 |
// Apply the style individually for each row (this allow us to apply the same font family to all rows keeping the individual row formatting. e.g. color) |
214 |
for(int i=initialRowNumber; i<=finalRowNumber; i++){ |
215 |
|
216 |
currentCursor.setPosition(currentTextEdit->document()->findBlockByLineNumber(i).position()); |
217 |
currentCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); |
218 |
|
219 |
QTextCharFormat format = currentCursor.charFormat(); |
220 |
|
221 |
if(desiredFormatType == FormatType::BOLD){ |
222 |
if(format.fontWeight() == QFont::Bold){ |
223 |
format.setFontWeight(QFont::Normal); |
224 |
} |
225 |
else{ |
226 |
format.setFontWeight(QFont::Bold); |
227 |
} |
228 |
} |
229 |
else if(desiredFormatType == FormatType::ITALIC){ |
230 |
if(format.fontItalic()){ |
231 |
format.setFontItalic(false); |
232 |
} |
233 |
else{ |
234 |
format.setFontItalic(true); |
235 |
} |
236 |
} |
237 |
else if(desiredFormatType == FormatType::COLOR){ |
238 |
QColor pickedColor = QColorDialog::getColor(Qt::green); |
239 |
|
240 |
if(pickedColor.isValid()){ |
241 |
format.setForeground(pickedColor); |
242 |
} |
243 |
} |
244 |
else if(desiredFormatType == FormatType::FONT_TYPE){ |
245 |
format.setFontFamily(ui->fcbFont->currentFont().family()); |
246 |
} |
247 |
else if(desiredFormatType == FormatType::FONT_SIZE){ |
248 |
bool ok; |
249 |
double newFontSize = ui->cbTextSize->currentText().toDouble(&ok); |
250 |
|
251 |
if(!ok){ |
252 |
Util::Dialogs::showError("The inputted font size is not valid!"); |
253 |
ui->cbTextSize->setCurrentIndex(0); // reset to default number |
254 |
return; |
255 |
} |
256 |
|
257 |
format.setFontPointSize(static_cast<qreal>(newFontSize)); |
258 |
} |
259 |
|
260 |
currentCursor.mergeCharFormat(format); |
261 |
} |
262 |
} |
263 |
} |
264 |
|
265 |
|
266 |
void WmFormatPage::on_pbAddPageAfter_clicked() |
267 |
{ |
268 |
addPage(true); |
269 |
} |
270 |
|
271 |
void WmFormatPage::on_pbAddPageBefore_clicked() |
272 |
{ |
273 |
addPage(false); |
274 |
} |
275 |
|
276 |
void WmFormatPage::on_pbDeleteCurrentPage_clicked() |
277 |
{ |
278 |
if(ui->twPages->count() <= 1){ |
279 |
Util::Dialogs::showError("You must have at least one page!"); |
280 |
return; |
281 |
} |
282 |
|
283 |
if(Util::Dialogs::showQuestion(this, "Are you sure do you want to delete the current page?")){ |
284 |
int indexToDelete = ui->twPages->currentIndex(); |
285 |
|
286 |
currentPages.removeAt(indexToDelete); |
287 |
ui->twPages->removeTab(indexToDelete); |
288 |
updateTabNames(indexToDelete); |
289 |
} |
290 |
} |
291 |
|
292 |
void WmFormatPage::updateTabNames(int startIndex){ |
293 |
// Update all other tab texts |
294 |
for(int i=startIndex; i<ui->twPages->count(); i++){ |
295 |
ui->twPages->setTabText(i, "Page " + QString::number(i+1)); |
296 |
} |
297 |
} |
298 |
|
299 |
void WmFormatPage::setCurrentFormatting(int index){ |
300 |
|
301 |
QFont defaultFont; |
302 |
defaultFont.setFamily(ui->fcbFont->currentText()); |
303 |
defaultFont.setPointSize(ui->cbTextSize->currentText().toInt()); |
304 |
|
305 |
this->currentPages[index]->getMainText().setFont(defaultFont); |
306 |
this->currentPages[index]->getFooterText().setFont(defaultFont); |
307 |
} |
308 |
|
309 |
void WmFormatPage::setDefaultOniFont(int index){ |
310 |
|
311 |
QFont defaultFont; |
312 |
defaultFont.setFamily("Tahoma"); |
313 |
defaultFont.setPointSize(12); |
314 |
|
315 |
this->currentPages[index]->getMainText().setFont(defaultFont); |
316 |
defaultFont.setPointSize(10); |
317 |
this->currentPages[index]->getFooterText().setFont(defaultFont); |
318 |
|
319 |
this->currentPages[index]->getMainText().setTextColor(Qt::green); |
320 |
this->currentPages[index]->getMainText().setFontWeight(QFont::Bold); |
321 |
this->currentPages[index]->getFooterText().setTextColor(Qt::green); |
322 |
this->currentPages[index]->getFooterText().setFontWeight(QFont::Bold); |
323 |
} |
324 |
|
325 |
QList<std::shared_ptr<WmPage> >& WmFormatPage::getCurrentPages(){ |
326 |
return this->currentPages; |
327 |
} |
328 |
|
329 |
// Ignore enter key in this page, so user can do enter after write the custom font |
330 |
void WmFormatPage::keyPressEvent(QKeyEvent * event){ |
331 |
|
332 |
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter){ |
333 |
return; |
334 |
} |
335 |
|
336 |
|
337 |
QWizardPage::keyPressEvent(event); |
338 |
} |
339 |
|
340 |
|