kleopatra
selftestdialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <config-kleopatra.h>
00034
00035 #include "selftestdialog.h"
00036
00037 #include "ui_selftestdialog.h"
00038
00039 #include <selftest/selftest.h>
00040
00041 #include <QAbstractTableModel>
00042 #include <QHeaderView>
00043
00044 #include <boost/shared_ptr.hpp>
00045
00046 #include <cassert>
00047 #include <vector>
00048
00049 using namespace Kleo;
00050 using namespace Kleo::Dialogs;
00051 using namespace boost;
00052
00053 namespace {
00054
00055 class Model : public QAbstractTableModel {
00056 Q_OBJECT
00057 public:
00058 explicit Model( QObject * parent=0 )
00059 : QAbstractTableModel( parent ),
00060 m_tests()
00061 {
00062
00063 }
00064
00065 enum Column {
00066 TestName,
00067 TestResult,
00068
00069 NumColumns
00070 };
00071
00072 const shared_ptr<SelfTest> & fromModelIndex( const QModelIndex & idx ) const {
00073 const unsigned int row = idx.row();
00074 if ( row < m_tests.size() )
00075 return m_tests[row];
00076 static const shared_ptr<SelfTest> null;
00077 return null;
00078 }
00079
00080 int rowCount( const QModelIndex & idx ) const { return idx.isValid() ? 0 : m_tests.size() ; }
00081 int columnCount( const QModelIndex & ) const { return NumColumns; }
00082
00083 QVariant data( const QModelIndex & idx, int role ) const {
00084 const unsigned int row = idx.row();
00085 if ( idx.isValid() && row < m_tests.size() )
00086 switch ( role ) {
00087 case Qt::DisplayRole:
00088 case Qt::ToolTipRole:
00089 switch ( idx.column() ) {
00090 case TestName:
00091 return m_tests[row]->name();
00092 case TestResult:
00093 if ( m_tests[row]->passed() )
00094 return i18n("Passed");
00095 else
00096 if ( role == Qt::ToolTipRole )
00097 return m_tests[row]->longError();
00098 else
00099 return m_tests[row]->skipped()
00100 ? i18n("Skipped")
00101 : m_tests[row]->shortError();
00102 }
00103 break;
00104 case Qt::BackgroundRole:
00105 return QColor( m_tests[row]->skipped() ? Qt::yellow :
00106 m_tests[row]->passed() ? Qt::green :
00107 Qt::red );
00108 }
00109 return QVariant();
00110 }
00111
00112 QVariant headerData( int section, Qt::Orientation o, int role ) const {
00113 if ( o == Qt::Horizontal &&
00114 section >= 0 && section < NumColumns &&
00115 role == Qt::DisplayRole )
00116 switch ( section ) {
00117 case TestName: return i18n("Test Name");
00118 case TestResult: return i18n("Result");
00119 }
00120 return QVariant();
00121 }
00122
00123 void clear() {
00124 if ( m_tests.empty() )
00125 return;
00126 beginRemoveRows( QModelIndex(), 0, m_tests.size() - 1 );
00127 m_tests.clear();
00128 endRemoveRows();
00129 }
00130
00131 void append( const std::vector< shared_ptr<SelfTest> > & tests ) {
00132 if ( tests.empty() )
00133 return;
00134 beginInsertRows( QModelIndex(), m_tests.size(), m_tests.size() + tests.size() );
00135 m_tests.insert( m_tests.end(), tests.begin(), tests.end() );
00136 endInsertRows();
00137 }
00138
00139 void reloadData() {
00140 if ( !m_tests.empty() )
00141 emit dataChanged( index( 0, 0 ), index( m_tests.size() - 1, NumColumns - 1 ) );
00142 }
00143
00144 const shared_ptr<SelfTest> & at( unsigned int idx ) const {
00145 return m_tests.at( idx );
00146 }
00147
00148 private:
00149 std::vector< shared_ptr<SelfTest> > m_tests;
00150 };
00151
00152 }
00153
00154 class SelfTestDialog::Private {
00155 friend class ::Kleo::Dialogs::SelfTestDialog;
00156 SelfTestDialog * const q;
00157 public:
00158 explicit Private( SelfTestDialog * qq )
00159 : q( qq ),
00160 ui( q )
00161 {
00162 ui.resultsTV->setModel( &model );
00163
00164 connect( ui.resultsTV->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
00165 q, SLOT(slotSelectionChanged()) );
00166 ui.proposedCorrectiveActionLB->setTextFormat( Qt::RichText );
00167 }
00168
00169 private:
00170 void slotSelectionChanged() {
00171 const int row = selectedRowIndex();
00172 if ( row < 0 ) {
00173 ui.proposedCorrectiveActionLB->setText( i18n("(select test first)" ) );
00174 ui.proposedCorrectiveActionGB->setEnabled( false );
00175 } else {
00176 ui.proposedCorrectiveActionGB->setEnabled( true );
00177 ui.proposedCorrectiveActionLB->setText( model.at(row)->proposedFix() );
00178 ui.doItPB->setEnabled( !model.at(row)->passed() && model.at(row)->canFixAutomatically() );
00179 }
00180 }
00181 void slotDoItClicked() {
00182 if ( const shared_ptr<SelfTest> st = model.fromModelIndex( selectedRow() ) )
00183 if ( st->fix() )
00184 model.reloadData();
00185 }
00186
00187 private:
00188 void updateColumnSizes() {
00189 ui.resultsTV->header()->resizeSections( QHeaderView::ResizeToContents );
00190 }
00191
00192 private:
00193 QModelIndex selectedRow() const {
00194 const QItemSelectionModel * const ism = ui.resultsTV->selectionModel();
00195 if ( !ism )
00196 return QModelIndex();
00197 const QModelIndexList mil = ism->selectedRows();
00198 return mil.empty() ? QModelIndex() : mil.front() ;
00199 }
00200 int selectedRowIndex() const {
00201 return selectedRow().row();
00202 }
00203
00204 private:
00205 Model model;
00206
00207 struct UI : public Ui_SelfTestDialog {
00208
00209 QPushButton * rerunPB;
00210
00211 explicit UI( SelfTestDialog * qq )
00212 : Ui_SelfTestDialog(),
00213 rerunPB( new QPushButton( i18n("Rerun Tests") ) )
00214 {
00215 setupUi( qq );
00216
00217 buttonBox->addButton( rerunPB, QDialogButtonBox::ActionRole );
00218 buttonBox->button( QDialogButtonBox::Ok )->setText( i18n("Continue") );
00219
00220 connect( rerunPB, SIGNAL(clicked()),
00221 qq, SIGNAL(updateRequested()) );
00222 }
00223 } ui;
00224 };
00225
00226 SelfTestDialog::SelfTestDialog( QWidget * p, Qt::WindowFlags f )
00227 : QDialog( p, f ), d( new Private( this ) )
00228 {
00229 setAutomaticMode( false );
00230 }
00231
00232 SelfTestDialog::SelfTestDialog( const std::vector< shared_ptr<SelfTest> > & tests, QWidget * p, Qt::WindowFlags f )
00233 : QDialog( p, f ), d( new Private( this ) )
00234 {
00235 addSelfTests( tests );
00236 setAutomaticMode( false );
00237 }
00238
00239 SelfTestDialog::~SelfTestDialog() {}
00240
00241 void SelfTestDialog::clear() {
00242 d->model.clear();
00243 }
00244
00245 void SelfTestDialog::addSelfTest( const shared_ptr<SelfTest> & test ) {
00246 d->model.append( std::vector< shared_ptr<SelfTest> >( 1, test ) );
00247 d->updateColumnSizes();
00248 }
00249
00250 void SelfTestDialog::addSelfTests( const std::vector< shared_ptr<SelfTest> > & tests ) {
00251 d->model.append( tests );
00252 d->updateColumnSizes();
00253 }
00254
00255 void SelfTestDialog::setRunAtStartUp( bool on ) {
00256 d->ui.runAtStartUpCB->setChecked( on );
00257 }
00258
00259 bool SelfTestDialog::runAtStartUp() const {
00260 return d->ui.runAtStartUpCB->isChecked();
00261 }
00262
00263 void SelfTestDialog::setAutomaticMode( bool automatic ) {
00264 d->ui.buttonBox->button( QDialogButtonBox::Ok )->setVisible( automatic );
00265 d->ui.buttonBox->button( QDialogButtonBox::Cancel )->setVisible( automatic );
00266 d->ui.buttonBox->button( QDialogButtonBox::Close )->setVisible( !automatic );
00267 }
00268
00269 #include "selftestdialog.moc"
00270 #include "moc_selftestdialog.cpp"