]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew-gui/MainWindow.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew-gui / MainWindow.cpp
1 /************************************************************************\r
2 **\r
3 **  Copyright (C) 2010  Strahinja Markovic\r
4 **\r
5 **  This file is part of FlightCrew.\r
6 **\r
7 **  FlightCrew is free software: you can redistribute it and/or modify\r
8 **  it under the terms of the GNU Lesser General Public License as published\r
9 **  by the Free Software Foundation, either version 3 of the License, or\r
10 **  (at your option) any later version.\r
11 **\r
12 **  FlightCrew is distributed in the hope that it will be useful,\r
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 **  GNU Lesser General Public License for more details.\r
16 **\r
17 **  You should have received a copy of the GNU Lesser General Public License\r
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.\r
19 **\r
20 *************************************************************************/\r
21 \r
22 #include <QSettings>\r
23 #include <QMessageBox>\r
24 #include <QFileDialog>\r
25 #include <QComboBox>\r
26 #include <QTableWidgetItem>\r
27 #include <QBrush>\r
28 #include <QDragEnterEvent>\r
29 #include <QDropEvent>\r
30 #include <QUrl>\r
31 #include <QtDebug>\r
32 #include "MainWindow.h"\r
33 #include <flightcrew.h>\r
34 namespace fc = FlightCrew;\r
35 \r
36 static const QString SETTINGS_GROUP = "mainwindow";\r
37 static const QBrush WARNING_BRUSH = QBrush( QColor( 255, 255, 230 ) );\r
38 static const QBrush ERROR_BRUSH   = QBrush( QColor( 255, 230, 230 ) );\r
39 static const QString FLIGHTCREW_VERSION = QString( FLIGHTCREW_FULL_VERSION );\r
40 \r
41 MainWindow::MainWindow( QWidget*, Qt::WFlags )\r
42 {\r
43     ui.setupUi( this );\r
44     \r
45     // We want to have the version showing in the window title.\r
46     setWindowTitle( "FlightCrew - " + FLIGHTCREW_VERSION );\r
47     \r
48     // Telling Qt to delete this window\r
49     // from memory when it is closed\r
50     setAttribute( Qt::WA_DeleteOnClose );\r
51     setAcceptDrops( true );\r
52 \r
53     // Needs to come before signals connect\r
54     // (avoiding side-effects)\r
55     ReadSettings();\r
56     ConnectSignalsToSlots();\r
57 }\r
58 \r
59 \r
60 MainWindow::~MainWindow()\r
61 {\r
62     WriteSettings();\r
63 }\r
64 \r
65 \r
66 void MainWindow::dragEnterEvent( QDragEnterEvent *event )\r
67 {\r
68     if ( event->mimeData()->hasFormat( "text/uri-list" ) )\r
69 \r
70         event->acceptProposedAction();\r
71 }\r
72 \r
73 \r
74 void MainWindow::dropEvent( QDropEvent *event )\r
75 {\r
76     if ( !event->mimeData()->hasUrls() )\r
77 \r
78         return;\r
79 \r
80     event->acceptProposedAction();\r
81 \r
82     QList< QUrl > urls = event->mimeData()->urls();\r
83 \r
84     if ( urls.empty() )\r
85 \r
86         return;\r
87 \r
88     AddFilenameToComboBox( urls[ 0 ].toLocalFile() );\r
89     StartValidation();\r
90 }\r
91 \r
92 \r
93 void MainWindow::StartValidation()\r
94 {\r
95     QString current_file = ui.FilepathsCombo->currentText();\r
96 \r
97     if ( QFileInfo( current_file ).suffix().toLower() != "epub" )\r
98     {\r
99         QMessageBox::critical( this,\r
100                                tr( "FlightCrew-gui" ),\r
101                                tr( "The specified file does not appear to be an epub." )\r
102                               );\r
103         return;\r
104     }\r
105 \r
106     if ( !QFileInfo( current_file ).exists() )\r
107     {\r
108         QMessageBox::critical( this,\r
109                                tr( "FlightCrew-gui" ),\r
110                                tr( "The specified file does not exist." )\r
111                               );\r
112         return;\r
113     }\r
114 \r
115     ui.ResultTable->clearContents();\r
116     std::vector< fc::Result > results;\r
117 \r
118     QApplication::setOverrideCursor( Qt::WaitCursor );\r
119 \r
120     try\r
121     {\r
122         results = fc::ValidateEpub( current_file.toUtf8().constData() );\r
123     }\r
124 \r
125     catch ( std::exception& exception )\r
126     {\r
127         // TODO: extract boost exception info\r
128         QMessageBox::critical( this,\r
129                                tr( "FlightCrew-gui" ),\r
130                                tr( "An exception occurred: %1." )\r
131                                .arg( QString::fromStdString( exception.what() ) )\r
132                               );\r
133         return;\r
134     }\r
135 \r
136     QApplication::restoreOverrideCursor();\r
137 \r
138     DisplayResults( results );\r
139 }\r
140 \r
141 \r
142 void MainWindow::BrowseForEpub()\r
143 {\r
144     QString filename = QFileDialog::getOpenFileName( this,\r
145                                                      tr( "Open File" ),\r
146                                                      m_LastFolderOpen,\r
147                                                      tr( "Epub files (*.epub)" )\r
148                                                    );\r
149 \r
150     if ( !filename.isEmpty() )\r
151     {\r
152         // Store the folder the user opened from\r
153         m_LastFolderOpen = QFileInfo( filename ).absolutePath();\r
154 \r
155         AddFilenameToComboBox( filename );        \r
156     }\r
157 }\r
158 \r
159 \r
160 void MainWindow::DisplayResults( const std::vector< fc::Result > &results )\r
161 {\r
162     ui.ResultTable->clear();\r
163 \r
164     if ( results.empty() )\r
165     {\r
166         DisplayNoProblemsMessage();\r
167         return;\r
168     }\r
169     \r
170     ConfigureTableForResults();\r
171 \r
172     for ( unsigned int i = 0; i < results.size(); ++i )\r
173     {\r
174         fc::Result result = results[ i ];\r
175 \r
176         ui.ResultTable->insertRow( ui.ResultTable->rowCount() );\r
177 \r
178         QBrush row_brush = result.GetResultType() == fc::ResultType_WARNING ?\r
179                            WARNING_BRUSH                                    :\r
180                            ERROR_BRUSH;\r
181 \r
182         QTableWidgetItem *item = NULL;\r
183         item = new QTableWidgetItem( QString::fromUtf8( result.GetFilepath().c_str() ) );\r
184         item->setBackground( row_brush );\r
185         ui.ResultTable->setItem( i, 0, item );\r
186 \r
187         item = result.GetErrorLine() > 0                                        ?\r
188                new QTableWidgetItem( QString::number( result.GetErrorLine() ) ) :\r
189                new QTableWidgetItem( tr( "N/A" ) );\r
190 \r
191         item->setBackground( row_brush );\r
192         ui.ResultTable->setItem( i, 1, item );\r
193 \r
194         item = new QTableWidgetItem( QString::fromUtf8( result.GetMessage().c_str() ) );\r
195         item->setBackground( row_brush );\r
196         ui.ResultTable->setItem( i, 2, item );\r
197     }\r
198 \r
199     // We first force the line number column \r
200     // to the smallest needed size...\r
201     ui.ResultTable->resizeColumnToContents( 0 );\r
202 \r
203     // ... and now the file column can be widened.\r
204     ui.ResultTable->resizeColumnToContents( 1 );\r
205 }\r
206 \r
207 \r
208 void MainWindow::DisplayNoProblemsMessage()\r
209 {\r
210     ui.ResultTable->setRowCount( 1 );\r
211     ui.ResultTable->setColumnCount( 1 );\r
212     ui.ResultTable->setHorizontalHeaderLabels( \r
213         QStringList() << tr( "Message" ) );\r
214 \r
215     QTableWidgetItem *item = new QTableWidgetItem( tr( "No problems found!" ) );\r
216     item->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );\r
217     \r
218     QFont font = item->font();\r
219     font.setPointSize( 16 );\r
220     item->setFont( font );\r
221     \r
222     ui.ResultTable->setItem( 0, 0, item );\r
223     ui.ResultTable->resizeRowToContents( 0 );\r
224 }\r
225 \r
226 \r
227 void MainWindow::AddFilenameToComboBox( const QString &filename )\r
228 {\r
229     int index = ui.FilepathsCombo->findText( filename );\r
230 \r
231     if ( index == -1 )            \r
232     {\r
233         ui.FilepathsCombo->insertItem( 0, filename );\r
234         ui.FilepathsCombo->setCurrentIndex( 0 );\r
235     }\r
236 \r
237     else\r
238     {\r
239         ui.FilepathsCombo->setCurrentIndex( index );\r
240     }\r
241 }\r
242 \r
243 \r
244 void MainWindow::ReadSettings()\r
245 {\r
246     QSettings settings;\r
247     settings.beginGroup( SETTINGS_GROUP );\r
248 \r
249     // The size of the window and it's full screen status\r
250     QByteArray geometry = settings.value( "geometry" ).toByteArray();\r
251 \r
252     if ( !geometry.isNull() )\r
253 \r
254         restoreGeometry( geometry );\r
255 \r
256     ui.FilepathsCombo->addItems( settings.value( "path_strings" ).toStringList() );\r
257     m_LastFolderOpen = settings.value( "lastfolderopen" ).toString();\r
258 \r
259     ui.FilepathsCombo->setCurrentIndex( settings.value( "lastusedcomboindex" ).toInt() );\r
260 }\r
261 \r
262 \r
263 void MainWindow::WriteSettings()\r
264 {\r
265     QSettings settings;\r
266     settings.beginGroup( SETTINGS_GROUP );\r
267 \r
268     // The size of the window and it's full screen status\r
269     settings.setValue( "geometry", saveGeometry() );\r
270 \r
271     QStringList path_strings;\r
272 \r
273     for ( int i = 0; i < ui.FilepathsCombo->count(); ++i )\r
274     {\r
275         path_strings.append( ui.FilepathsCombo->itemText( i ) );\r
276     }\r
277 \r
278     settings.setValue( "path_strings", path_strings );\r
279     settings.setValue( "lastusedcomboindex", ui.FilepathsCombo->currentIndex() );\r
280     settings.setValue( "lastfolderopen", m_LastFolderOpen );\r
281 }\r
282 \r
283 \r
284 void MainWindow::ConfigureTableForResults()\r
285 {\r
286     ui.ResultTable->setRowCount( 0 );\r
287     ui.ResultTable->setColumnCount( 3 );\r
288     ui.ResultTable->setHorizontalHeaderLabels( \r
289         QStringList() << tr( "File" ) << tr( "Line" ) << tr( "Message" ) );\r
290     ui.ResultTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );\r
291 }\r
292 \r
293 \r
294 void MainWindow::ConnectSignalsToSlots()\r
295 {\r
296     connect( ui.actionExit,   SIGNAL( triggered() ), qApp, SLOT( closeAllWindows() ) );\r
297     connect( ui.GoButton,     SIGNAL( clicked()   ), this, SLOT( StartValidation() ) );\r
298     connect( ui.BrowseButton, SIGNAL( clicked()   ), this, SLOT( BrowseForEpub()   ) );\r
299 }\r
300 \r
301 \r
302 \r