test
[push-it] / src / showfulllistdialog.cpp
1 /*
2     Copyright (C) <2010>  <Markus Scharnowski markus.scharnowski@gmail.com>
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <QPushButton>
18 #include <QTextBrowser>
19 #include <QHBoxLayout>
20 #include <QFileDialog>
21 #include <QMessageBox>
22
23 #include <iostream>
24 #include <fstream>
25
26 #include "showfulllistdialog.hpp"
27
28 ShowFullListDialog::ShowFullListDialog(QWidget *parent, QString text) :
29     QDialog(parent)
30 {
31   QPushButton *doneButton = new QPushButton(tr("&Done"));
32   QPushButton *saveButton = new QPushButton(tr("&Save"));
33   QPushButton *saveEditedButton = new QPushButton(tr("S&ave edited version"));
34   textEdit = new QTextBrowser;
35   textEdit->setReadOnly(false);
36   textEdit->setText(text);
37   initialString = text;
38
39   QVBoxLayout *layout = new QVBoxLayout;
40   layout->addWidget(doneButton);
41   layout->addWidget(saveButton);
42   layout->addWidget(saveEditedButton);
43   layout->addStretch();
44
45   QHBoxLayout *mainLayout = new QHBoxLayout;
46   mainLayout->addLayout(layout);
47   mainLayout->addWidget(textEdit);
48
49   connect(doneButton, SIGNAL(clicked()), this, SLOT(deleteLater()));
50   connect(saveButton, SIGNAL(clicked()), this, SLOT(save()));
51   connect(saveEditedButton, SIGNAL(clicked()), this, SLOT(saveEdited()));
52
53   setLayout(mainLayout);
54   setWindowTitle(tr("History"));
55   setBaseSize(700,400);
56   setMinimumWidth(600);
57   setMinimumHeight(400);
58   hide();
59 }
60
61 ShowFullListDialog::~ShowFullListDialog()
62 {
63 }
64
65 int ShowFullListDialog::saveToFile(QString string)
66 {
67   QString fileName = QFileDialog::getSaveFileName(0,
68                                                   tr("Save to"), "",
69                                                   tr("Push-It textfile (*.pushit);;All Files (*.*)"));
70   if (fileName.isEmpty())
71   {
72     return EXIT_FAILURE;
73   }
74   else
75   {
76     std::ofstream myfile;
77     myfile.open(fileName.toStdString().c_str());
78     if (!myfile.is_open())
79     {
80       QMessageBox::information(0, tr("Unable to open file"),
81                                tr("Unable to open file"));
82       return EXIT_FAILURE;
83     }
84     myfile << string.toStdString();
85     myfile.close();
86   }
87   return EXIT_SUCCESS;
88 }
89
90 void ShowFullListDialog::save()
91 {
92   saveToFile(initialString);
93 }
94
95 void ShowFullListDialog::saveEdited()
96 {
97   saveToFile(textEdit->toPlainText());
98 }