1 /************************************************************************
\r
3 ** Copyright (C) 2010 Strahinja Markovic
\r
5 ** This file is part of FlightCrew.
\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
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
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
20 *************************************************************************/
\r
22 #include <QSettings>
\r
23 #include <QMessageBox>
\r
24 #include <QFileDialog>
\r
25 #include <QComboBox>
\r
26 #include <QTableWidgetItem>
\r
28 #include <QDragEnterEvent>
\r
29 #include <QDropEvent>
\r
32 #include "MainWindow.h"
\r
33 #include <flightcrew.h>
\r
34 namespace fc = FlightCrew;
\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
41 MainWindow::MainWindow( QWidget*, Qt::WFlags )
\r
45 // We want to have the version showing in the window title.
\r
46 setWindowTitle( "FlightCrew - " + FLIGHTCREW_VERSION );
\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
53 // Needs to come before signals connect
\r
54 // (avoiding side-effects)
\r
56 ConnectSignalsToSlots();
\r
60 MainWindow::~MainWindow()
\r
66 void MainWindow::dragEnterEvent( QDragEnterEvent *event )
\r
68 if ( event->mimeData()->hasFormat( "text/uri-list" ) )
\r
70 event->acceptProposedAction();
\r
74 void MainWindow::dropEvent( QDropEvent *event )
\r
76 if ( !event->mimeData()->hasUrls() )
\r
80 event->acceptProposedAction();
\r
82 QList< QUrl > urls = event->mimeData()->urls();
\r
88 AddFilenameToComboBox( urls[ 0 ].toLocalFile() );
\r
93 void MainWindow::StartValidation()
\r
95 QString current_file = ui.FilepathsCombo->currentText();
\r
97 if ( QFileInfo( current_file ).suffix().toLower() != "epub" )
\r
99 QMessageBox::critical( this,
\r
100 tr( "FlightCrew-gui" ),
\r
101 tr( "The specified file does not appear to be an epub." )
\r
106 if ( !QFileInfo( current_file ).exists() )
\r
108 QMessageBox::critical( this,
\r
109 tr( "FlightCrew-gui" ),
\r
110 tr( "The specified file does not exist." )
\r
115 ui.ResultTable->clearContents();
\r
116 std::vector< fc::Result > results;
\r
118 QApplication::setOverrideCursor( Qt::WaitCursor );
\r
122 results = fc::ValidateEpub( current_file.toUtf8().constData() );
\r
125 catch ( std::exception& exception )
\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
136 QApplication::restoreOverrideCursor();
\r
138 DisplayResults( results );
\r
142 void MainWindow::BrowseForEpub()
\r
144 QString filename = QFileDialog::getOpenFileName( this,
\r
147 tr( "Epub files (*.epub)" )
\r
150 if ( !filename.isEmpty() )
\r
152 // Store the folder the user opened from
\r
153 m_LastFolderOpen = QFileInfo( filename ).absolutePath();
\r
155 AddFilenameToComboBox( filename );
\r
160 void MainWindow::DisplayResults( const std::vector< fc::Result > &results )
\r
162 ui.ResultTable->clear();
\r
164 if ( results.empty() )
\r
166 DisplayNoProblemsMessage();
\r
170 ConfigureTableForResults();
\r
172 for ( unsigned int i = 0; i < results.size(); ++i )
\r
174 fc::Result result = results[ i ];
\r
176 ui.ResultTable->insertRow( ui.ResultTable->rowCount() );
\r
178 QBrush row_brush = result.GetResultType() == fc::ResultType_WARNING ?
\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
187 item = result.GetErrorLine() > 0 ?
\r
188 new QTableWidgetItem( QString::number( result.GetErrorLine() ) ) :
\r
189 new QTableWidgetItem( tr( "N/A" ) );
\r
191 item->setBackground( row_brush );
\r
192 ui.ResultTable->setItem( i, 1, item );
\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
199 // We first force the line number column
\r
200 // to the smallest needed size...
\r
201 ui.ResultTable->resizeColumnToContents( 0 );
\r
203 // ... and now the file column can be widened.
\r
204 ui.ResultTable->resizeColumnToContents( 1 );
\r
208 void MainWindow::DisplayNoProblemsMessage()
\r
210 ui.ResultTable->setRowCount( 1 );
\r
211 ui.ResultTable->setColumnCount( 1 );
\r
212 ui.ResultTable->setHorizontalHeaderLabels(
\r
213 QStringList() << tr( "Message" ) );
\r
215 QTableWidgetItem *item = new QTableWidgetItem( tr( "No problems found!" ) );
\r
216 item->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
\r
218 QFont font = item->font();
\r
219 font.setPointSize( 16 );
\r
220 item->setFont( font );
\r
222 ui.ResultTable->setItem( 0, 0, item );
\r
223 ui.ResultTable->resizeRowToContents( 0 );
\r
227 void MainWindow::AddFilenameToComboBox( const QString &filename )
\r
229 int index = ui.FilepathsCombo->findText( filename );
\r
231 if ( index == -1 )
\r
233 ui.FilepathsCombo->insertItem( 0, filename );
\r
234 ui.FilepathsCombo->setCurrentIndex( 0 );
\r
239 ui.FilepathsCombo->setCurrentIndex( index );
\r
244 void MainWindow::ReadSettings()
\r
246 QSettings settings;
\r
247 settings.beginGroup( SETTINGS_GROUP );
\r
249 // The size of the window and it's full screen status
\r
250 QByteArray geometry = settings.value( "geometry" ).toByteArray();
\r
252 if ( !geometry.isNull() )
\r
254 restoreGeometry( geometry );
\r
256 ui.FilepathsCombo->addItems( settings.value( "path_strings" ).toStringList() );
\r
257 m_LastFolderOpen = settings.value( "lastfolderopen" ).toString();
\r
259 ui.FilepathsCombo->setCurrentIndex( settings.value( "lastusedcomboindex" ).toInt() );
\r
263 void MainWindow::WriteSettings()
\r
265 QSettings settings;
\r
266 settings.beginGroup( SETTINGS_GROUP );
\r
268 // The size of the window and it's full screen status
\r
269 settings.setValue( "geometry", saveGeometry() );
\r
271 QStringList path_strings;
\r
273 for ( int i = 0; i < ui.FilepathsCombo->count(); ++i )
\r
275 path_strings.append( ui.FilepathsCombo->itemText( i ) );
\r
278 settings.setValue( "path_strings", path_strings );
\r
279 settings.setValue( "lastusedcomboindex", ui.FilepathsCombo->currentIndex() );
\r
280 settings.setValue( "lastfolderopen", m_LastFolderOpen );
\r
284 void MainWindow::ConfigureTableForResults()
\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
294 void MainWindow::ConnectSignalsToSlots()
\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