jpg support added
[wpcreator] / src / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "progressdialog.h"
4
5 #include <QFileInfo>
6
7
8 MainWindow::MainWindow(QWidget *parent)
9     : QMainWindow(parent), ui(new Ui::MainWindowClass)
10 {
11     ui->setupUi(this);
12     ui->set->setVisible(false);
13 }
14
15 MainWindow::~MainWindow()
16 {
17     delete ui;
18 }
19
20
21 void MainWindow::openFile () {
22     QFileDialog *dialog = new QFileDialog(this,QString(),QString(),"*.png *.jpg");
23     connect (dialog,SIGNAL(fileSelected(QString)),this,SLOT(getImage(QString)));
24     dialog->show();
25 }
26
27
28 void MainWindow::getImage (QString image) {
29     QTextStream out (stdout);
30     out << image << endl;
31     basicImage = new QPixmap (image);
32     if (!(basicImage->height()==480&&basicImage->width()==3200)){
33         out << "show Warning!" << endl;
34         basicImage = new QPixmap (basicImage->scaled(3200,480));
35     }
36     //else {
37     QPixmap showImage = basicImage->scaled(640,96);
38     out << basicImage->height() << endl;
39     QLabel *origLabel = new QLabel (ui->originalImage);
40     origLabel->setGeometry (0,0,650,110);
41     origLabel->setPixmap (showImage);
42     ui->crop->setEnabled(true);
43     origLabel->show();
44     //}
45 }
46
47 void MainWindow::cropImage () {
48     img_1 = new QPixmap(basicImage->copy(0,0,800,480));
49     QLabel *img1Label = new QLabel (ui->image_1);
50     img1Label->setPixmap(img_1->scaled(160,96));
51     img1Label->setGeometry (0,0,160,99);
52     img1Label->show();
53
54     img_2 = new QPixmap(basicImage->copy(800,0,800,480));
55     QLabel *img2Label = new QLabel (ui->image_2);
56     img2Label->setPixmap(img_2->scaled(160,96));
57     img2Label->setGeometry (0,0,160,99);
58     img2Label->show();
59
60     img_3 = new QPixmap(basicImage->copy(1600,0,800,480));
61     QLabel *img3Label = new QLabel (ui->image_3);
62     img3Label->setPixmap(img_3->scaled(160,96));
63     img3Label->setGeometry (0,0,160,99);
64     img3Label->show();
65
66     img_4 = new QPixmap(basicImage->copy(2400,0,800,480));
67     QLabel *img4Label = new QLabel (ui->image_4);
68     img4Label->setPixmap(img_4->scaled(160,96));
69     img4Label->setGeometry (0,0,160,99);
70     img4Label->show();
71
72     ui->install->setEnabled(true);
73
74 }
75
76 void MainWindow::installImageSet () {
77     nameDialog =new NameDialog(this);
78     nameDialog->setWindowTitle ("Choose a name");
79     connect (nameDialog, SIGNAL (nameEntered(QString)),this,SLOT(installImageSetWithName(QString)));
80     nameDialog->show();
81 }
82
83
84 void MainWindow::installImageSetWithName(QString name) {
85     QTextStream out (stdout);
86     nameDialog->close();
87     ProgressDialog *progress = new ProgressDialog (this);
88     connect (this,SIGNAL (installationStatusUpdate(int)),progress,SLOT(updateInstallationStatus(int)));
89     connect (this,SIGNAL (installationFinished()),progress,SLOT(installationFinished()));
90     progress->show();
91     #ifdef Q_WS_HILDON
92         QString base = "/home/user/MyDocs/.images/";
93     #else
94         QString base = "./";
95     #endif
96         QString filename = base +name;
97         out << filename;
98         if (QFileInfo (filename+"1.png").exists()) {
99             int i = 1;
100             QString basic_filename (filename);
101             while (QFileInfo (filename+"-1.png").exists()) {
102                 filename=basic_filename+"-"+QString::number(i);
103                 i=i+1;
104             }
105         }
106         emit installationStatusUpdate(10);
107         bool suc = false;
108         suc = img_1->save(filename+"-1.png");
109         if (suc){
110             emit installationStatusUpdate(30);
111             suc = img_2->save(filename+"-2.png");
112         }
113         if (suc) {
114             emit installationStatusUpdate(50);
115             suc = img_3->save(filename+"-3.png");
116         }
117         if (suc) {
118             emit installationStatusUpdate(70);
119             suc = img_4->save(filename+"-4.png");
120         }
121         if (suc) {
122             emit installationStatusUpdate(90);
123             //create install file
124             QFile file (base + "."+name+".desktop");
125             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
126                 out << "fileerror " + file.fileName()<< endl;
127                 //todo
128             }
129             QTextStream file_writer (&file);
130             file_writer << "[Desktop Entry]\nType=Background Image\nName="+name+"\nHidden=true" << endl;
131             file_writer << "X-File1="+filename+"-1.png" << endl;
132             file_writer << "X-File2="+filename+"-2.png" << endl;
133             file_writer << "X-File3="+filename+"-3.png" << endl;
134             file_writer << "X-File4="+filename+"-4.png" << endl;
135             file_writer << "X-Order=1" << endl;
136             file.close();
137             emit installationFinished();
138         }
139         else {
140             //error
141         }
142         disconnect (this,SIGNAL (installationStatusUpdate(int)),progress,SLOT(updateInstallationStatus(int)));
143         disconnect (this,SIGNAL (installationFinished()),progress,SLOT(installationFinished()));
144
145 }
146