Debian Codegraph @

MainWindow.cpp

raw
1 #include "MainWindow.h"
2 #include "ui_MainWindow.h"
3
4 #include <QBitmap>
5 #include <QPainter>
6 #include <QFileDialog>
7 #include <QScrollBar>
8 #include <QDir>
9 #include <QStandardPaths>
10 #include <QMessageBox>
11 #include <QRgb>
12 #include <QBuffer>
13 #include <QScreen>
14 #include <QStandardPaths>
15 #include <QDir>
16 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
17 #include <QRandomGenerator>
18 #endif
19
20 #include <iostream>
21 #include <cstdlib>
22
23 #include "config_puzzle.h"
24 #include "FormAbout.h"
25
26 int puzzleOrigWidth = 200;
27 int puzzleOrigHeight = 165;
28
29 int puzzleBorderWidth = 30;
30 int puzzleBorderHeight = 28;
31
32 int puzzleWidth = puzzleOrigWidth-57; // 143
33 int puzzleHeight = puzzleOrigHeight-57; // 108
34 int distanceForAutoBonding = 15;
35
36 // puzzle jigsaw
37
38 MainWindow::MainWindow(QWidget *parent) :
39 QMainWindow(parent)
40 {
41 this->resize(1024,768);
42 this->move((qApp->primaryScreen()->size().width()-this->width())/2,(qApp->primaryScreen()->size().height()-this->height())/2);
43
44 this->setWindowTitle(tr("Puzzle jigsaw v%1").arg(QString(PUZZLE_VERSION)));
45 this->setWindowIcon(QIcon(QString(PATH_USERDATA)+"/images/puzzle.png"));
46
47 conf = new QSettings(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)+"/puzzle-jigsaw.ini", QSettings::IniFormat);
48
49
50 selectItem=nullptr;
51 isMoveWidgetTable=false;
52
53 background = new QLabel(this);
54 background->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/table.png"));
55 background->setGeometry(0,0,this->width(),this->height());
56 background->setScaledContents(true);
57 background->show();
58 background->lower();
59
60 // central widget
61 centralWidget = new QLabel(this);
62
63 // widwet for item puzzle
64 widgetTable = new QLabel(centralWidget);
65 widgetTable->installEventFilter(this);
66 widgetTable->setFrameShape(QFrame::Box);
67 widgetTable->setGeometry(-5000,-5000,10000,10000);
68
69 // background form buttons
70 backgroundButtons = new QLabel(this);
71 backgroundButtons->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/background.png"));
72 backgroundButtons->setScaledContents(true);
73 backgroundButtons->installEventFilter(this);
74
75
76 btnNewPuzzle = new QLabel(this);
77 btnNewPuzzle->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/new.png"));
78 btnNewPuzzle->setToolTip(tr("Open file")+" (Ctrl+O)");
79 btnNewPuzzle->setGeometry(0,32,80,80);
80 btnNewPuzzle->setScaledContents(true);
81 btnNewPuzzle->installEventFilter(this);
82
83 btnSavePuzzle = new QLabel(this);
84 btnSavePuzzle->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/save.png"));
85 btnSavePuzzle->setToolTip(tr("Save puzzle to file")+" (Ctrl+S)");
86 btnSavePuzzle->setScaledContents(true);
87 btnSavePuzzle->setGeometry(0,124,80,80);
88 btnSavePuzzle->installEventFilter(this);
89
90 btnLoadPuzzle = new QLabel(this);
91 btnLoadPuzzle->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/open.png"));
92 btnLoadPuzzle->setToolTip(tr("Load puzzle from file")+" (Ctrl+L)");
93 btnLoadPuzzle->setScaledContents(true);
94 btnLoadPuzzle->setGeometry(0,216,80,80);
95 btnLoadPuzzle->installEventFilter(this);
96
97 btnAlignment = new QLabel(this);
98 btnAlignment->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/alignment.png"));
99 btnAlignment->setToolTip(tr("Alignment puzzle")+" (Ctrl+N)");
100 btnAlignment->setScaledContents(true);
101 btnAlignment->setGeometry(0,308,80,80);
102 btnAlignment->installEventFilter(this);
103
104 btnPreview = new QLabel(this);
105 btnPreview->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/preview.png"));
106 btnPreview->setToolTip(tr("Show/hide preview")+" (Ctrl+P)");
107 btnPreview->setGeometry(0,400,80,80);
108 btnPreview->setScaledContents(true);
109 btnPreview->installEventFilter(this);
110
111 btnAbout = new QLabel(this);
112 btnAbout->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/about.png"));
113 btnAbout->setToolTip(tr("About")+" (Ctrl+I)");
114 btnAbout->setScaledContents(true);
115 btnAbout->setGeometry(0,492,80,80);
116 btnAbout->installEventFilter(this);
117
118 btnExit = new QLabel(this);
119 btnExit->setPixmap(QPixmap(QString(PATH_USERDATA)+"/images/buttons/exit.png"));
120 btnExit->setToolTip(tr("Exit")+" (Ctrl+Q)");
121 btnExit->setScaledContents(true);
122 btnExit->setGeometry(0,600,80,80);
123 btnExit->installEventFilter(this);
124
125
126
127 preview = new WidgetPreview(this);
128 preview->hide();
129
130 boxMessage = new BoxMessage(this);
131 boxMessage->setGeometry((this->width()-700)/2-50,250,700,200);
132 boxMessage->setText(tr("Choose an image\n for the puzzle"));
133 boxMessage->show();
134 connect(boxMessage,&BoxMessage::clicked,this,&MainWindow::newPuzzle);
135
136 }
137
138 MainWindow::~MainWindow(){
139
140 }
141
142 // Creating a puzzle from an puzzlePixmap
143 void MainWindow::createPuzzle(){
144 this->setWindowTitle(QString(tr("Puzzle [%1]")).arg(puzzleFilename));
145
146 int countX = puzzlePixmap.width()/puzzleWidth;
147 int countY = puzzlePixmap.height()/puzzleHeight;
148
149 QPixmap pixPreview = puzzlePixmap.copy(0,0,countX*puzzleWidth,countY*puzzleHeight);
150 preview->setPicture(pixPreview);
151
152
153 for (auto *item:listItems) delete item;
154 listItems.clear();
155
156 QString typePuzzle = "1";
157 int zOrder=0;
158
159 for (int x=0;x<countX;x++){
160 for (int y=0;y<countY;y++){
161 typePuzzle="1"; // default
162
163 if (x % 2 != 0 and y % 2 ==0){
164 typePuzzle="2";
165 }
166 if (x % 2 == 0 and y % 2 !=0){
167 typePuzzle="2";
168 }
169
170
171 if (x==0){
172 if (y==0){
173 typePuzzle="1-l-t";
174 }else{
175 if (y % 2 == 0){
176 typePuzzle="1-l";
177 }else{
178 typePuzzle="2-l";
179 }
180 }
181 }
182 if (y==0){
183 if (x==0){
184 }else{
185 if (x % 2 == 0){
186 typePuzzle="1-t";
187 }else{
188 typePuzzle="2-t";
189 }
190 }
191 }
192 if (x==countX-1){
193 if (y==0){
194 if (countX % 2 == 0){
195 typePuzzle="2-r-t";
196 }else{
197 typePuzzle="1-r-t";
198 }
199 }else{
200 if ((y % 2 == 0 and countX % 2 ==0) or (y % 2 != 0 and countX % 2!=0)){
201 typePuzzle="2-r";
202 }else{
203 typePuzzle="1-r";
204 }
205 }
206 }
207 if (y==countY-1){
208 if (x==0){
209 if (countY % 2 == 0 ){
210 typePuzzle="2-l-b";
211 }else{
212 typePuzzle="1-l-b";
213 }
214 }else{
215 if ((x % 2 ==0 and countY % 2 == 0) or (x % 2 !=0 and countY % 2 != 0)){
216 typePuzzle="2-b";
217 }else{
218 typePuzzle="1-b";
219 }
220 }
221 }
222 if (x==countX-1 and y==countY-1){
223 if ((countX % 2 == 0 and countY % 2 == 0) or (countX % 2 != 0 and countY % 2 != 0)){
224 typePuzzle="1-r-b";
225 }else{
226 typePuzzle="2-r-b";
227 }
228 }
229
230
231
232 // create item puzzle
233 QLabel *puzzle = new QLabel(widgetTable);
234 puzzle->setGeometry(x*(puzzleWidth+20),10+y*(puzzleHeight+20),puzzleOrigWidth,puzzleOrigHeight);
235 puzzle->setScaledContents(true);
236 puzzle->setProperty("cell_x",x);
237 puzzle->setProperty("cell_y",y);
238 puzzle->setProperty("type_puzzle",typePuzzle);
239 puzzle->setProperty("zOrder",zOrder);
240 setPicturePuzzle(puzzle,"effect1");
241 puzzle->show();
242 listItems.push_back(std::move(puzzle));
243 zOrder++;
244 }
245 }
246 }
247
248
249 // Setting the shape of the puzzle on the image
250 void MainWindow::setPicturePuzzle(QLabel *item, const QString &effect){
251 QImage pix_temp = puzzlePixmap.toImage();
252 QPixmap pix(pix_temp.width()+puzzleOrigWidth*2,pix_temp.height()+puzzleOrigHeight*2);
253
254 QPainter painterPix(&pix);
255 painterPix.drawImage(puzzleBorderWidth,puzzleBorderHeight,pix_temp,0,0);
256 painterPix.end();
257
258 QString typePuzzle = item->property("type_puzzle").toString();
259 int cell_x = item->property("cell_x").toInt();
260 int cell_y = item->property("cell_y").toInt();
261
262 QPixmap puzzle_mask(QString(PATH_USERDATA)+"/images/pieces/piece"+typePuzzle+".png");
263 QImage puzzle_top(QString(PATH_USERDATA)+"/images/pieces/piece"+typePuzzle+"_"+effect+".png");
264
265 QPixmap temp = pix.copy(cell_x*puzzleWidth,cell_y*puzzleHeight,puzzleOrigWidth,puzzleOrigHeight);
266 temp.setMask(puzzle_mask.createMaskFromColor(Qt::black,Qt::MaskOutColor));
267 QPainter p(&temp);
268 p.drawImage(0,0,puzzle_top.copy(0,0,puzzleOrigWidth,puzzleOrigHeight),0,0);
269 p.end();
270
271 item->setPixmap(temp);
272
273 }
274
275 void MainWindow::resizeEvent(QResizeEvent */*e*/){
276 centralWidget->setGeometry(0,0,this->width(),this->height());
277
278 backgroundButtons->setGeometry(this->width()-134, 0, 134,this->height());
279
280 btnNewPuzzle->move(this->width()-100,btnNewPuzzle->y());
281 btnSavePuzzle->move(this->width()-100,btnSavePuzzle->y());
282 btnLoadPuzzle->move(this->width()-100,btnLoadPuzzle->y());
283 btnAlignment->move(this->width()-100,btnAlignment->y());
284 btnAbout->move(this->width()-100,btnAbout->y());
285 btnPreview->move(this->width()-100,btnPreview->y());
286 btnExit->move(this->width()-100,btnExit->y());
287
288
289 background->setGeometry(0,0,this->width(),this->height());
290 }
291
292
293 bool MainWindow::eventFilter(QObject *obj, QEvent *event){
294 QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
295
296 if (event->type() == QEvent::MouseButtonPress){
297 // bug KDE?
298 if (obj==backgroundButtons or obj==btnPreview or
299 obj==btnNewPuzzle or obj==btnSavePuzzle or
300 obj==btnLoadPuzzle or obj==btnAlignment or
301 obj==btnAbout or obj==btnExit
302 ){
303 buttonAction="click";
304 mousePosition=QPointF(mouseEvent->globalX(),mouseEvent->globalY());
305 return true;
306 }
307
308 if (obj==widgetTable){
309 if(mouseEvent->buttons() == Qt::LeftButton){
310 QLabel *itemPuzzle = nullptr;
311 int zOrder=0;
312 for (auto &it:listItems){
313 if (mouseEvent->x()>it->x() and mouseEvent->x()<it->x()+it->width()
314 and mouseEvent->y()>it->y() and mouseEvent->y()<it->y()+it->height()
315 ){
316 QString typePuzzle = it->property("type_puzzle").toString();
317 QImage puzzle_mask(QString(PATH_USERDATA)+"/images/pieces/piece"+typePuzzle+".png");
318 QRgb rgb = puzzle_mask.pixel(mouseEvent->x()- it->x(),mouseEvent->y()-it->y());
319 if (qAlpha(rgb)!=0){
320 if (it->property("zOrder").toInt()>zOrder or itemPuzzle == nullptr){
321 itemPuzzle=it;
322 zOrder=it->property("zOrder").toInt();
323 }
324 }
325 }
326 }
327
328 if (itemPuzzle!=nullptr){
329 selectItem=itemPuzzle;
330 mousePosition=QPointF(mouseEvent->globalX(),mouseEvent->globalY());
331 selectItem->raise();
332
333 int selectZOrder=selectItem->property("zOrder").toInt();
334 for (auto &it:listItems){
335 if (it->property("zOrder").toInt()>selectZOrder){
336 it->setProperty("zOrder",it->property("zOrder").toInt()-1);
337 }
338 }
339 selectItem->setProperty("zOrder",listItems.size());
340 setPicturePuzzle(selectItem,"effect2");
341 }
342 }
343
344 if(mouseEvent->buttons() == Qt::MiddleButton or mouseEvent->buttons() == Qt::RightButton){
345 mousePositionWidgetTable=QPointF(mouseEvent->globalX(),mouseEvent->globalY());
346 isMoveWidgetTable=true;
347 }
348
349 return true;
350 }
351 }
352
353 if (event->type() == QEvent::MouseButtonRelease){
354 if (selectItem!=nullptr){
355 setPicturePuzzle(selectItem,"effect1");
356 dockItem(selectItem);
357 if (isWon()){
358 for (auto &it:listItems){
359 setPicturePuzzle(it,"");
360 }
361
362 boxMessage->setGeometry((this->width()-500)/2,(this->height()-100)/2,500,100);
363 boxMessage->setText(tr("You win!"));
364 boxMessage->show();
365 }
366 }
367
368
369 if (buttonAction=="click"){
370 if (obj==btnPreview){
371 if (!preview->isVisible()){
372 preview->show();
373 preview->move(this->width()-preview->width()-20,this->height()-preview->height()-20);
374 }else{
375 preview->hide();
376 }
377 }
378
379 if (obj==btnNewPuzzle){
380 newPuzzle();
381 }
382 if (obj==btnSavePuzzle){
383 savePuzzle();
384 }
385 if (obj==btnLoadPuzzle){
386 loadPuzzle();
387 }
388 if (obj==btnAlignment){
389 newAlignment();
390 }
391
392 if (obj==btnAbout){
393 about();
394 }
395
396 if (obj==btnExit){
397 QApplication::closeAllWindows();
398 close();
399 }
400 }
401
402 selectItem=nullptr;
403 isMoveWidgetTable=false;
404 }
405
406 if (event->type() == QEvent::MouseMove){
407 if (mousePosition!=QPointF(mouseEvent->globalX(),mouseEvent->globalY())){
408 buttonAction="move";
409 }
410 if (selectItem!=nullptr){
411 selectItem->move(selectItem->x()+ (mouseEvent->globalX()-mousePosition.x()),selectItem->y()+ (mouseEvent->globalY()-mousePosition.y()));
412 mousePosition=QPointF(mouseEvent->globalX(),mouseEvent->globalY());
413 }
414 if (isMoveWidgetTable){
415 widgetTable->move(widgetTable->x()+ (mouseEvent->globalX()-mousePositionWidgetTable.x()),widgetTable->y()+ (mouseEvent->globalY()-mousePositionWidgetTable.y()));
416 mousePositionWidgetTable=QPointF(mouseEvent->globalX(),mouseEvent->globalY());
417 }
418 }
419
420 return QObject::eventFilter(obj, event);
421 }
422
423 void MainWindow::wheelEvent(QWheelEvent */*event*/){
424
425 }
426
427 void MainWindow::keyPressEvent(QKeyEvent *event){
428 if (event->key()==Qt::Key_Up){
429 widgetTable->move(widgetTable->x(),widgetTable->y()+32);
430
431 }
432 if (event->key()==Qt::Key_Down){
433 widgetTable->move(widgetTable->x(),widgetTable->y()-32);
434
435 }
436 if (event->key()==Qt::Key_Right){
437 widgetTable->move(widgetTable->x()-32,widgetTable->y());
438
439 }
440 if (event->key()==Qt::Key_Left){
441 widgetTable->move(widgetTable->x()+32,widgetTable->y());
442 }
443
444
445 // open image
446 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_O){
447 newPuzzle();
448 }
449
450 // save puzzle
451 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_S){
452 savePuzzle();
453 }
454
455 // load puzzle
456 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_L){
457 loadPuzzle();
458 }
459
460 // preview
461 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_P){
462 if (!preview->isVisible()){
463 preview->show();
464 preview->move(this->width()-preview->width()-20,this->height()-preview->height()-20);
465 }else{
466 preview->hide();
467 }
468 }
469
470 // alignment
471 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_N){
472 newAlignment();
473
474 }
475
476 // about
477 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_I){
478 about();
479 }
480
481 // exit
482 if (event->modifiers()==Qt::ControlModifier && event->key()==Qt::Key_Q){
483 close();
484 }
485 }
486
487 bool MainWindow::isWon(){
488 for (QLabel *item:listItems){
489 int itemCellX=item->property("cell_x").toInt();
490 int itemCellY=item->property("cell_y").toInt();
491
492 for (QLabel *item2:listItems){
493 int item2CellX=item2->property("cell_x").toInt();
494 int item2CellY=item2->property("cell_y").toInt();
495
496 if (itemCellX+1==item2CellX and itemCellY==item2CellY){
497 if (item->pos().x()+puzzleWidth-3!=item2->pos().x() or item->pos().y()!=item2->pos().y()){
498 return false;
499 }
500 }
501 if (itemCellX==item2CellX and itemCellY+1==item2CellY){
502 if (item->pos().x()!=item2->pos().x() or item->pos().y()+puzzleHeight!=item2->pos().y()){
503 return false;
504 }
505 }
506 }
507 }
508
509 return true;
510 }
511
512 void MainWindow::dockItem(QLabel *pItem){
513 int selectCellX=pItem->property("cell_x").toInt();
514 int selectCellY=pItem->property("cell_y").toInt();
515
516 int minDistance = distanceForAutoBonding+1;
517 int newPosItemX = pItem->x();
518 int newPosItemY = pItem->y();
519
520 for (QLabel *item:listItems){
521 int itemCellX=item->property("cell_x").toInt();
522 int itemCellY=item->property("cell_y").toInt();
523
524 if (selectCellX-1==itemCellX and selectCellY==itemCellY){
525 if (item->pos().x()+(puzzleWidth)+distanceForAutoBonding>pItem->pos().x()
526 and item->pos().x()+(puzzleWidth)-distanceForAutoBonding<pItem->pos().x()
527 and item->pos().y()+distanceForAutoBonding>pItem->pos().y()
528 and item->pos().y()-distanceForAutoBonding<pItem->pos().y()
529 ){
530 int distanse = abs(pItem->pos().x()-(item->pos().x()+(puzzleWidth))) + abs(pItem->pos().y()-item->pos().y());
531 if (distanse<minDistance){
532 minDistance = distanse;
533 newPosItemX=item->pos().x()+(puzzleWidth)-3;
534 newPosItemY=item->pos().y();
535 }
536 }
537 }
538
539 if (selectCellX+1==itemCellX and selectCellY==itemCellY){
540 if (item->pos().x()-(puzzleWidth)+distanceForAutoBonding>pItem->pos().x()
541 and item->pos().x()-(puzzleWidth)-distanceForAutoBonding<pItem->pos().x()
542 and item->pos().y()+distanceForAutoBonding>pItem->pos().y()
543 and item->pos().y()-distanceForAutoBonding<pItem->pos().y()
544 ){
545 int distanse = abs(pItem->pos().x()-(item->pos().x()-(puzzleWidth))) + abs(pItem->pos().y()-item->pos().y());
546 if (distanse<minDistance){
547 minDistance = distanse;
548 newPosItemX=item->pos().x()-(puzzleWidth)+3;
549 newPosItemY=item->pos().y();
550 }
551 }
552 }
553
554 if (selectCellX==itemCellX and selectCellY-1==itemCellY){
555 if (item->pos().x()+distanceForAutoBonding>pItem->pos().x()
556 and item->pos().x()-distanceForAutoBonding<pItem->pos().x()
557 and item->pos().y()+(puzzleHeight)+distanceForAutoBonding>pItem->pos().y()
558 and item->pos().y()+(puzzleHeight)-distanceForAutoBonding<pItem->pos().y()
559 ){
560 int distanse = abs(pItem->pos().x()-item->pos().x()) + abs(pItem->pos().y()-(item->pos().y()+(puzzleHeight)));
561 if (distanse<minDistance){
562 minDistance = distanse;
563 newPosItemX=item->pos().x();
564 newPosItemY=item->pos().y()+(puzzleHeight);
565 }
566 }
567 }
568
569 if (selectCellX==itemCellX and selectCellY+1==itemCellY){
570 if (item->pos().x()+distanceForAutoBonding>pItem->pos().x()
571 and item->pos().x()-distanceForAutoBonding<pItem->pos().x()
572 and item->pos().y()-(puzzleHeight)+distanceForAutoBonding>pItem->pos().y()
573 and item->pos().y()-(puzzleHeight)-distanceForAutoBonding<pItem->pos().y()
574 ){
575 int distanse = abs(pItem->pos().x()-item->pos().x()) + abs(pItem->pos().y()-(item->pos().y()-(puzzleHeight)));
576 if (distanse<minDistance){
577 minDistance = distanse;
578 newPosItemX=item->pos().x();
579 newPosItemY=item->pos().y()-(puzzleHeight);
580 }
581 }
582 }
583 }
584
585 pItem->move(newPosItemX,newPosItemY);
586 }
587
588 // open image from file (new game)
589 void MainWindow::newPuzzle(){
590
591 QString lastPath = conf->value("global/lastpath",QString(PATH_USERDATA)+"/samples").toString();
592
593 QString filename = QFileDialog::getOpenFileName(this,tr("open images"),lastPath,tr("Images (*.png *.xpm *.jpg *.jpeg *.tiff *.webp *.bmp)"));
594 if (filename.isEmpty()) return;
595
596 conf->setValue("global/lastpath",QFileInfo(filename).path());
597
598 disconnect(boxMessage,&BoxMessage::clicked,this,&MainWindow::newPuzzle);
599
600
601 puzzleFilename = QFileInfo(filename).fileName();
602 puzzlePixmap = QPixmap(filename);
603
604 QStringList pairs = puzzleFilename.split(".");
605 pairs.pop_back();
606 puzzleFilename = pairs.join(".");
607
608 createPuzzle();
609 boxMessage->hide();
610
611 newAlignment();
612 }
613
614 // new alignment
615 void MainWindow::newAlignment(){
616 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
617 QRandomGenerator *gen = QRandomGenerator::system();
618 #endif
619
620 widgetTable->setGeometry(-5000,-5000,10000,10000);
621
622 for (auto *item:listItems){
623 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
624 quint32 x = gen->bounded(0,centralWidget->width()-puzzleWidth-100);
625 quint32 y = gen->bounded(0,centralWidget->height()-puzzleHeight-100);
626 #else
627 quint32 x=qrand()%(centralWidget->width()-puzzleWidth-100);
628 quint32 y=qrand()%(centralWidget->height()-puzzleHeight-100);
629 #endif
630 item->move(0-widgetTable->x()+x,0-widgetTable->y()+y);
631
632 setPicturePuzzle(item,"effect1");
633 }
634 boxMessage->hide();
635 }
636
637
638 // save game to file
639 void MainWindow::savePuzzle(){
640 QDir dir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)+"/puzzle-jigsaw/");
641 if (!dir.exists()){
642 dir.mkpath(dir.path());
643 }
644
645 QString filename = QFileDialog::getSaveFileName(this,tr("save puzzle"),dir.path(),tr("*.puzzle"));
646 if (filename.isEmpty()) return;
647 if (QFileInfo(filename).suffix().isEmpty()){
648 filename.append(".puzzle");
649 }
650
651 QFile file(filename);
652 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
653 return;
654 }
655 QTextStream out(&file);
656
657 QString postions;
658
659 out << puzzleFilename << "\n";
660
661 QByteArray bArray;
662 QBuffer buffer(&bArray);
663 buffer.open(QIODevice::WriteOnly);
664 puzzlePixmap.save(&buffer,"PNG");
665
666 out << bArray.toHex() << "\n";
667 for (auto &item:listItems){
668 postions.append(QString::number(item->x())+","+QString::number(item->y())+';');
669 }
670 out << postions << "\n";
671 out << widgetTable->x() << ";" << widgetTable->y() << "\n";
672
673 file.close();
674
675 }
676
677 // load game from file
678 void MainWindow::loadPuzzle(){
679 QDir dir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)+"/puzzle-jigsaw/");
680 if (!dir.exists()){
681 dir.mkpath(dir.path());
682 }
683
684 QString filename = QFileDialog::getOpenFileName(this,tr("load puzzle"),dir.path(),tr("*.puzzle"));
685 if (filename.isEmpty()) return;
686
687 QFile file(filename);
688 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
689 QMessageBox::critical(this,tr("puzzle"),QString(tr("Can't open the file %1")).arg(filename));
690 return;
691 }
692
693 puzzleFilename = file.readLine();
694 puzzlePixmap.loadFromData(QByteArray::fromHex(file.readLine()));
695 QStringList positions = QString(file.readLine()).split(";");
696 QStringList positionWidgetTable = QString(file.readLine()).split(";");
697
698 puzzleFilename.replace("\n","");
699
700
701 // position widgetTable
702 if (positionWidgetTable.size()==2){
703 widgetTable->move(positionWidgetTable.at(0).toInt(),positionWidgetTable.at(1).toInt());
704 }
705
706
707 createPuzzle();
708
709 // set position for puzzles
710 for (int i=0;i<positions.size()-1;i++){
711 if (i>=listItems.size()) break;
712 QStringList pair = positions.at(i).split(",");
713 if (pair.size()!=2) continue;
714
715 int x = pair.at(0).toInt();
716 int y = pair.at(1).toInt();
717
718 listItems.at(i)->move(x,y);
719 }
720
721 file.close();
722 boxMessage->hide();
723 }
724
725 void MainWindow::about(){
726 FormAbout about;
727 about.exec();
728 }