putting project-files up
[wpcreator] / src / mainwindow.cpp
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
new file mode 100644 (file)
index 0000000..961df17
--- /dev/null
@@ -0,0 +1,146 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+#include "progressdialog.h"
+
+#include <QFileInfo>
+
+
+MainWindow::MainWindow(QWidget *parent)
+    : QMainWindow(parent), ui(new Ui::MainWindowClass)
+{
+    ui->setupUi(this);
+    ui->set->setVisible(false);
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}
+
+
+void MainWindow::openFile () {
+    QFileDialog *dialog = new QFileDialog(this,QString(),QString(),"*.png");
+    connect (dialog,SIGNAL(fileSelected(QString)),this,SLOT(getImage(QString)));
+    dialog->show();
+}
+
+
+void MainWindow::getImage (QString image) {
+    QTextStream out (stdout);
+    out << image << endl;
+    basicImage = new QPixmap (image);
+    if (!(basicImage->height()==480&&basicImage->width()==3200)){
+        out << "show Warning!" << endl;
+        basicImage = &basicImage->scaled(3200,480);
+    }
+    //else {
+    QPixmap showImage = basicImage->scaled(640,96);
+    out << basicImage->height() << endl;
+    QLabel *origLabel = new QLabel (ui->originalImage);
+    origLabel->setGeometry (0,0,650,110);
+    origLabel->setPixmap (showImage);
+    ui->crop->setEnabled(true);
+    origLabel->show();
+    //}
+}
+
+void MainWindow::cropImage () {
+    img_1 = new QPixmap(basicImage->copy(0,0,800,480));
+    QLabel *img1Label = new QLabel (ui->image_1);
+    img1Label->setPixmap(img_1->scaled(160,96));
+    img1Label->setGeometry (0,0,160,99);
+    img1Label->show();
+
+    img_2 = new QPixmap(basicImage->copy(800,0,800,480));
+    QLabel *img2Label = new QLabel (ui->image_2);
+    img2Label->setPixmap(img_2->scaled(160,96));
+    img2Label->setGeometry (0,0,160,99);
+    img2Label->show();
+
+    img_3 = new QPixmap(basicImage->copy(1600,0,800,480));
+    QLabel *img3Label = new QLabel (ui->image_3);
+    img3Label->setPixmap(img_3->scaled(160,96));
+    img3Label->setGeometry (0,0,160,99);
+    img3Label->show();
+
+    img_4 = new QPixmap(basicImage->copy(2400,0,800,480));
+    QLabel *img4Label = new QLabel (ui->image_4);
+    img4Label->setPixmap(img_4->scaled(160,96));
+    img4Label->setGeometry (0,0,160,99);
+    img4Label->show();
+
+    ui->install->setEnabled(true);
+
+}
+
+void MainWindow::installImageSet () {
+    nameDialog =new NameDialog(this);
+    nameDialog->setWindowTitle ("Choose a name");
+    connect (nameDialog, SIGNAL (nameEntered(QString)),this,SLOT(installImageSetWithName(QString)));
+    nameDialog->show();
+}
+
+
+void MainWindow::installImageSetWithName(QString name) {
+    QTextStream out (stdout);
+    nameDialog->close();
+    ProgressDialog *progress = new ProgressDialog (this);
+    connect (this,SIGNAL (installationStatusUpdate(int)),progress,SLOT(updateInstallationStatus(int)));
+    connect (this,SIGNAL (installationFinished()),progress,SLOT(installationFinished()));
+    progress->show();
+    #ifdef Q_WS_HILDON
+        QString base = "/home/user/MyDocs/.images/";
+    #else
+        QString base = "/home/dwilms/testapp/";
+    #endif
+        QString filename = base +name;
+        out << filename;
+        if (QFileInfo (filename+"1.png").exists()) {
+            int i = 1;
+            QString basic_filename (filename);
+            while (QFileInfo (filename+"-1.png").exists()) {
+                filename=basic_filename+"-"+QString::number(i);
+                i=i+1;
+            }
+        }
+        emit installationStatusUpdate(10);
+        bool suc = false;
+        suc = img_1->save(filename+"-1.png");
+        if (suc){
+            emit installationStatusUpdate(30);
+            suc = img_2->save(filename+"-2.png");
+        }
+        if (suc) {
+            emit installationStatusUpdate(50);
+            suc = img_3->save(filename+"-3.png");
+        }
+        if (suc) {
+            emit installationStatusUpdate(70);
+            suc = img_4->save(filename+"-4.png");
+        }
+        if (suc) {
+            emit installationStatusUpdate(90);
+            //create install file
+            QFile file (base + "."+name+".desktop");
+            if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+                out << "fileerror " + file.fileName()<< endl;
+                //todo
+            }
+            QTextStream file_writer (&file);
+            file_writer << "[Desktop Entry]\nType=Background Image\nName="+name+"\nHidden=true" << endl;
+            file_writer << "X-File1="+filename+"-1.png" << endl;
+            file_writer << "X-File2="+filename+"-2.png" << endl;
+            file_writer << "X-File3="+filename+"-3.png" << endl;
+            file_writer << "X-File4="+filename+"-4.png" << endl;
+            file_writer << "X-Order=1" << endl;
+            file.close();
+            emit installationFinished();
+        }
+        else {
+            //error
+        }
+        disconnect (this,SIGNAL (installationStatusUpdate(int)),progress,SLOT(updateInstallationStatus(int)));
+        disconnect (this,SIGNAL (installationFinished()),progress,SLOT(installationFinished()));
+
+}
+