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