forked from lbellonda/ConfrontaPDF
-
Notifications
You must be signed in to change notification settings - Fork 1
/
saveform.cpp
143 lines (127 loc) · 4.85 KB
/
saveform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright © 2008-13 Qtrac Ltd. All rights reserved.
This program or module is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 2 of
the License, or (at your option) any later version. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
*/
#include "saveform.hpp"
#include "aboutform.hpp"
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QFormLayout>
#include <QGroupBox>
#include <QImageWriter>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QVBoxLayout>
SaveForm::SaveForm(const QString &path, QString *filename, bool *saveAll,
SavePages *savePages, QWidget *parent)
: QDialog(parent), m_path(path), m_filename(filename),
m_saveAll(saveAll), m_savePages(savePages)
{
createWidgets();
createLayout();
createConnections();
updateUi();
setWindowTitle(tr("%1 — Save As").arg(AboutForm::ProgramName));
}
void SaveForm::createWidgets()
{
saveGroupBox = new QGroupBox(tr("Pairs"));
saveCurrentRadioButton = new QRadioButton(tr("Current &Page Pair"));
saveAllRadioButton = new QRadioButton(tr("&All Page Pairs"));
if (*m_saveAll)
saveAllRadioButton->setChecked(true);
else
saveCurrentRadioButton->setChecked(true);
pagesGroupBox = new QGroupBox(tr("Pages"));
leftPagesRadioButton = new QRadioButton(tr("File #&1's Pages"));
rightPagesRadioButton = new QRadioButton(tr("File #&2's Pages"));
bothPagesRadioButton = new QRadioButton(tr("&Both Files' Pages"));
if (*m_savePages == SaveLeftPages)
leftPagesRadioButton->setChecked(true);
else if (*m_savePages == SaveRightPages)
rightPagesRadioButton->setChecked(true);
else // m_savePages == SaveBothPages
bothPagesRadioButton->setChecked(true);
filenameLabel = new QLabel(tr("&Filename:"));
filenameLineEdit = new QLineEdit;
filenameLineEdit->setText(*m_filename);
filenameLabel->setBuddy(filenameLineEdit);
chooseFileButton = new QPushButton(tr("B&rowse..."));
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|
QDialogButtonBox::Cancel);
}
void SaveForm::createLayout()
{
QHBoxLayout *saveLayout = new QHBoxLayout;
saveLayout->addWidget(saveCurrentRadioButton);
saveLayout->addWidget(saveAllRadioButton);
saveLayout->addStretch();
saveGroupBox->setLayout(saveLayout);
QHBoxLayout *pagesLayout = new QHBoxLayout;
pagesLayout->addWidget(leftPagesRadioButton);
pagesLayout->addWidget(rightPagesRadioButton);
pagesLayout->addWidget(bothPagesRadioButton);
pagesGroupBox->setLayout(pagesLayout);
QHBoxLayout *filenameLayout = new QHBoxLayout;
filenameLayout->addWidget(filenameLabel);
filenameLayout->addWidget(filenameLineEdit);
filenameLayout->addWidget(chooseFileButton);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(saveGroupBox);
layout->addWidget(pagesGroupBox);
layout->addLayout(filenameLayout);
layout->addStretch();
layout->addWidget(buttonBox);
setLayout(layout);
}
void SaveForm::createConnections()
{
connect(chooseFileButton, SIGNAL(clicked()), this, SLOT(chooseFile()));
connect(filenameLineEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(updateUi()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}
void SaveForm::updateUi()
{
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
!filenameLineEdit->text().isEmpty());
}
void SaveForm::chooseFile()
{
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
QStringList suffixes;
foreach (const QByteArray &format, formats)
suffixes << "*." + QString(format.toLower());
QString filename = QFileDialog::getSaveFileName(this,
tr("%1 — Browse").arg(AboutForm::ProgramName), m_path,
tr("PDF files (*.pdf);;Image files (%1)")
.arg(suffixes.join(" ")));
if (!filename.isEmpty()) {
filenameLineEdit->setText(filename);
buttonBox->button(QDialogButtonBox::Ok)->setFocus();
}
}
void SaveForm::accept()
{
*m_filename = filenameLineEdit->text();
if (!m_filename->contains("."))
*m_filename += ".pdf";
*m_saveAll = saveAllRadioButton->isChecked();
if (leftPagesRadioButton->isChecked())
*m_savePages = SaveLeftPages;
else if (rightPagesRadioButton->isChecked())
*m_savePages = SaveRightPages;
else // bothPagesRadioButton->isChecked()
*m_savePages = SaveBothPages;
QDialog::accept();
}