summaryrefslogtreecommitdiff
path: root/src/gui/exportdialog.cpp
blob: cc0be7b6fa6354a2359e9d24fac5bae1f6e48130 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "exportdialog.h"
#include "ui_exportdialog.h"

#include "tikzit.h"

#include <QFileDialog>
#include <QSettings>
#include <QStandardPaths>

ExportDialog::ExportDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ExportDialog)
{
    QSettings settings("tikzit", "tikzit");
    ui->setupUi(this);

    QIntValidator *v = new QIntValidator(this);
    v->setBottom(1);
    ui->width->setValidator(v);
    ui->height->setValidator(v);
    connect(ui->width, SIGNAL(editingFinished()),
            this, SLOT(setHeightFromWidth()));
    connect(ui->height, SIGNAL(editingFinished()),
            this, SLOT(setWidthFromHeight()));

    PdfDocument *doc = tikzit->previewWindow()->doc();
    if (doc) {
        QSize size = doc->size() * 4;
        ui->width->blockSignals(true);
        ui->height->blockSignals(true);
        ui->width->setText(QString::number(size.width()));
        ui->height->setText(QString::number(size.height()));
        ui->width->blockSignals(false);
        ui->height->blockSignals(false);
    }

    if (!settings.value("previous-export-file-format").isNull()) {
        ui->fileFormat->setCurrentIndex(settings.value("previous-export-file-format").toInt());
    }

    // set a default export file
    QString path = (!settings.value("previous-export-file-path").isNull()) ?
        settings.value("previous-export-file-path").toString() :
        QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

    QString suffix;
    switch (ui->fileFormat->currentIndex()) {
        case PNG: suffix = ".png"; break;
        case JPG: suffix = ".jpg"; break;
        case PDF: suffix = ".pdf"; break;
    }

    QString fileName;
    int i = 0;
    bool exists = true;
    while (exists) {
        fileName = path + "/tikzit_image" + QString::number(i) + suffix;
        exists = QFileInfo::exists(fileName);
        ++i;
    }
    ui->filePath->setText(QDir::toNativeSeparators(fileName));
}

ExportDialog::~ExportDialog()
{
    delete ui;
}

QString ExportDialog::filePath()
{
    return ui->filePath->text();
}

QSize ExportDialog::size()
{
    return QSize(ui->width->text().toInt(),
                 ui->height->text().toInt());
}

ExportDialog::Format ExportDialog::fileFormat()
{
    return static_cast<Format>(ui->fileFormat->currentIndex());
}

void ExportDialog::accept()
{
    QSettings settings("tikzit", "tikzit");
    QFileInfo fi(filePath());
    settings.setValue("previous-export-file-path", fi.absolutePath());
    settings.setValue("previous-export-file-format", fileFormat());
    QDialog::accept();
}

void ExportDialog::setHeightFromWidth()
{
    if (ui->keepAspect->isChecked()) {
        PdfDocument *doc = tikzit->previewWindow()->doc();
        if (doc == nullptr || doc->size().width() == 0 || doc->size().height() == 0) return;
        int w = ui->width->text().toInt();
        int h = (w * doc->size().height()) / doc->size().width();

        ui->height->blockSignals(true);
        ui->height->setText(QString::number(h));
        ui->height->blockSignals(false);
    }
}

void ExportDialog::setWidthFromHeight()
{
    if (ui->keepAspect->isChecked()) {
        PdfDocument *doc = tikzit->previewWindow()->doc();
        if (doc == nullptr || doc->size().width() == 0 || doc->size().height() == 0) return;
        int h = ui->height->text().toInt();
        int w = (h * doc->size().width()) / doc->size().height();

        ui->width->blockSignals(true);
        ui->width->setText(QString::number(w));
        ui->width->blockSignals(false);
    }
}

void ExportDialog::on_keepAspect_stateChanged(int state)
{
    if (state == Qt::Checked) setHeightFromWidth();
}

void ExportDialog::on_browseButton_clicked()
{
    QSettings settings("tikzit", "tikzit");

    QString suffix;
    switch (ui->fileFormat->currentIndex()) {
        case PNG: suffix = "png"; break;
        case JPG: suffix = "jpg"; break;
        case PDF: suffix = "pdf"; break;
    }

    QFileDialog dialog;
    dialog.setDefaultSuffix(suffix);
    dialog.setWindowTitle(tr("Export File Path"));
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    dialog.setNameFilter(ui->fileFormat->currentText());
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setLabelText(QFileDialog::Accept, "Select");

    QFileInfo fi(ui->filePath->text());
    if (!fi.absolutePath().isEmpty()) {
        dialog.setDirectory(fi.absolutePath());
        dialog.selectFile(fi.baseName());
    }

    dialog.setOption(QFileDialog::DontUseNativeDialog);

    if (dialog.exec()) {
        ui->filePath->setText(QDir::toNativeSeparators(dialog.selectedFiles()[0]));
    }
}

void ExportDialog::on_fileFormat_currentIndexChanged(int f)
{
    ui->width->setEnabled(f != PDF);
    ui->height->setEnabled(f != PDF);
    ui->keepAspect->setEnabled(f != PDF);

    QString path = ui->filePath->text();
    if (!path.isEmpty()) {
        QRegularExpression re("\\.[^.]*$");
        switch (f) {
            case PNG: path.replace(re, ".png"); break;
            case JPG: path.replace(re, ".jpg"); break;
            case PDF: path.replace(re, ".pdf"); break;
        }

        ui->filePath->setText(path);
    }
}