]> git.cworth.org Git - vogl/blob - src/vogleditor/vogleditor_qtrimdialog.cpp
UI: Update trim dialog so that it does not accept invalid input.
[vogl] / src / vogleditor / vogleditor_qtrimdialog.cpp
1 #include "vogleditor_qtrimdialog.h"
2 #include "ui_vogleditor_qtrimdialog.h"
3 #include <QFileDialog>
4 #include <QMessageBox>
5
6 vogleditor_QTrimDialog::vogleditor_QTrimDialog(QString parentTraceFile, uint maxFrameIndex, uint maxTrimLength, QWidget *parent) :
7     QDialog(parent),
8     ui(new Ui::vogleditor_QTrimDialog),
9     m_maxFrameIndex(maxFrameIndex),
10     m_maxTrimLength(maxTrimLength),
11     m_trim_frame("0"),
12     m_trim_len("1")
13 {
14     ui->setupUi(this);
15     ui->trimFrameLineEdit->setText(m_trim_frame);
16     ui->trimLenLineEdit->setText(m_trim_len);
17
18     QString trimFilename = parentTraceFile;
19     trimFilename.insert(trimFilename.lastIndexOf("."), "-trim");
20     ui->trimFileLineEdit->setText(trimFilename);
21 }
22
23 vogleditor_QTrimDialog::~vogleditor_QTrimDialog()
24 {
25     delete ui;
26 }
27
28 void vogleditor_QTrimDialog::on_buttonBox_clicked(QAbstractButton *button)
29 {
30     if (button == ui->buttonBox->button(QDialogButtonBox::Ok))
31     {
32         // validate the trim start frame
33         bool bValidFrame = false;
34         uint tmpFrame = ui->trimFrameLineEdit->text().toUInt(&bValidFrame);
35         bValidFrame = bValidFrame && (tmpFrame <= m_maxFrameIndex);
36
37         if (!bValidFrame)
38         {
39             QMessageBox::warning(this, tr("Invalid Trim Frame"), tr("Please enter a valid frame number at which to start the trim."),
40                                  QMessageBox::Ok, QMessageBox::Ok);
41             ui->trimFrameLineEdit->setFocus();
42             return;
43         }
44
45         // validate the trim length
46         bool bValidLen = false;
47         uint tmpLen = ui->trimLenLineEdit->text().toUInt(&bValidLen);
48         bValidLen = bValidLen && (tmpLen > 0 && tmpLen <= m_maxTrimLength);
49
50         if (!bValidLen)
51         {
52             QMessageBox::warning(this, tr("Invalid Trim Count"), tr("Please enter a valid nubmer of frames to trim."),
53                                  QMessageBox::Ok, QMessageBox::Ok);
54             ui->trimLenLineEdit->setFocus();
55             return;
56         }
57
58         // validate the filename
59         QFile file(ui->trimFileLineEdit->text());
60         if (file.exists())
61         {
62             QString message = ui->trimFileLineEdit->text();
63             message += " already exits.\nWould you like to overwrite it?";
64             int ret = QMessageBox::warning(this, tr("File Already Exists"), tr(message.toStdString().c_str()),
65                                  QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
66
67             if (ret == QMessageBox::No)
68             {
69                 // return so that the user can update the path
70                 ui->trimFileLineEdit->setFocus();
71                 return;
72             }
73         }
74
75         m_trim_frame = ui->trimFrameLineEdit->text();
76         m_trim_len = ui->trimLenLineEdit->text();
77         m_trim_file = ui->trimFileLineEdit->text();
78         accept();
79     }
80 }
81
82 void vogleditor_QTrimDialog::on_buttonBox_rejected()
83 {
84     reject();
85 }
86
87 void vogleditor_QTrimDialog::on_pickTrimFileButton_pressed()
88 {
89     // open file dialog
90     QString suggestedName = ui->trimFileLineEdit->text();
91     QString selectedName = QFileDialog::getSaveFileName(this, tr("Save Trim File"), suggestedName, tr("Trace file (*.bin)"));
92
93     if (!selectedName.isEmpty())
94     {
95         ui->trimFileLineEdit->setText(selectedName);
96     }
97 }
98
99 void vogleditor_QTrimDialog::on_trimLenLineEdit_textChanged(const QString &arg1)
100 {
101     bool bConverted = false;
102     uint trimLen = arg1.toUInt(&bConverted);
103
104     // make sure the length could be converted to a UINT, and that it isn't more than the allowed length
105     if (bConverted == false || trimLen > m_maxTrimLength)
106     {
107         // turn background red
108         QPalette palette(ui->trimLenLineEdit->palette());
109         palette.setColor(QPalette::Base, Qt::red);
110         ui->trimLenLineEdit->setPalette(palette);
111     }
112     else
113     {
114         // restore background color
115         QPalette palette(ui->trimLenLineEdit->palette());
116         palette.setColor(QPalette::Base, Qt::white);
117         ui->trimLenLineEdit->setPalette(palette);
118     }
119 }
120
121 void vogleditor_QTrimDialog::on_trimFrameLineEdit_textChanged(const QString &arg1)
122 {
123     bool bConverted = false;
124     uint trimFrame = arg1.toUInt(&bConverted);
125
126     // make sure frame could be converted to a UINT, and that it isn't greater than the number of frames in the trace
127     if (bConverted == false || trimFrame > m_maxFrameIndex)
128     {
129         // turn background red
130         QPalette palette(ui->trimFrameLineEdit->palette());
131         palette.setColor(QPalette::Base, Qt::red);
132         ui->trimFrameLineEdit->setPalette(palette);
133     }
134     else
135     {
136         // restore background color
137         QPalette palette(ui->trimFrameLineEdit->palette());
138         palette.setColor(QPalette::Base, Qt::white);
139         ui->trimFrameLineEdit->setPalette(palette);
140     }
141 }