2 * Copyright 2007-2009 Parker Coates <parker.coates@gmail.com>
4 * This file is part of Killbots.
6 * Killbots is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * Killbots is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Killbots. If not, see <http://www.gnu.org/licenses/>.
20 #include "rulesetselector.h"
23 #include "rulesetdetailsdialog.h"
27 #include <KDE/KLineEdit>
28 #include <KDE/KLocalizedString>
29 #include <KDE/KStandardDirs>
31 #include <QtGui/QGroupBox>
32 #include <QtGui/QLabel>
33 #include <QtGui/QLayout>
34 #include <QtGui/QListWidget>
35 #include <QtGui/QPushButton>
36 #include <QtGui/QScrollArea>
37 #include <QtGui/QTableWidget>
39 Killbots::RulesetSelector::RulesetSelector( QWidget
* parent
)
44 // Create a hidden KLineEdit to use the automatic KConfigXT connection
45 kcfg_Ruleset
= new KLineEdit();
46 kcfg_Ruleset
->setObjectName( "kcfg_Ruleset" );
49 m_listWidget
= new QListWidget();
50 m_listWidget
->setWhatsThis( i18n("A list of the Killbots rulesets installed on this computer.") );
52 QGroupBox
* groupBox
= new QGroupBox( i18n("Game Type Details") );
53 groupBox
->setWhatsThis( i18n("Lists information on the currently selected game type.") );
55 QLabel
* authorLabel
= new QLabel( i18n("Author:") );
56 authorLabel
->setAlignment( Qt::AlignRight
| Qt::AlignTop
);
58 m_author
= new QLabel();
59 m_author
->setAlignment( Qt::AlignLeft
| Qt::AlignTop
);
60 m_author
->setWordWrap( true );
62 QLabel
* contactLabel
= new QLabel( i18n("Contact:") );
63 contactLabel
->setAlignment( Qt::AlignRight
| Qt::AlignTop
);
65 m_authorContact
= new QLabel();
66 m_authorContact
->setAlignment( Qt::AlignLeft
| Qt::AlignTop
);
67 m_authorContact
->setWordWrap( true );
68 m_authorContact
->setOpenExternalLinks ( true );
70 QLabel
* descriptionLabel
= new QLabel( i18n("Description:") );
71 descriptionLabel
->setAlignment( Qt::AlignRight
| Qt::AlignTop
);
73 m_description
= new QLabel();
74 m_description
->setAlignment( Qt::AlignLeft
| Qt::AlignTop
);
75 m_description
->setWordWrap( true );
77 QPushButton
* detailsButton
= new QPushButton( i18n("Details...") );
78 detailsButton
->setToolTip( i18n("Show the detailed parameters of the selected game type") );
79 detailsButton
->setWhatsThis( i18n("Opens a dialog listing the values of all internal parameters for the selected game type.") );
81 QGridLayout
* boxLayout
= new QGridLayout( groupBox
);
82 boxLayout
->addWidget( authorLabel
, 1, 0 );
83 boxLayout
->addWidget( m_author
, 1, 1 );
84 boxLayout
->addWidget( contactLabel
, 2, 0 );
85 boxLayout
->addWidget( m_authorContact
, 2, 1 );
86 boxLayout
->addWidget( descriptionLabel
, 3, 0 );
87 boxLayout
->addWidget( m_description
, 3, 1 );
88 boxLayout
->addWidget( detailsButton
, 4, 1, Qt::AlignLeft
);
89 boxLayout
->setColumnStretch( 1, 10 );
90 boxLayout
->setRowStretch( 5, 10 );
92 QVBoxLayout
* layout
= new QVBoxLayout( this );
93 layout
->setMargin( 0 );
94 layout
->addWidget( kcfg_Ruleset
);
95 layout
->addWidget( m_listWidget
);
96 layout
->addWidget( groupBox
, 10 );
98 connect( m_listWidget
, SIGNAL(currentTextChanged(QString
)), this, SLOT(selectionChanged(QString
)) );
99 connect( detailsButton
, SIGNAL(clicked()), this, SLOT(showDetailsDialog()) );
105 Killbots::RulesetSelector::~RulesetSelector()
107 qDeleteAll( m_rulesetMap
);
111 void Killbots::RulesetSelector::findRulesets()
113 qDeleteAll( m_rulesetMap
);
114 m_rulesetMap
.clear();
116 m_listWidget
->clear();
117 m_listWidget
->setSortingEnabled( true );
119 QStringList fileList
;
120 KGlobal::dirs()->findAllResources ( "ruleset", "*.desktop", KStandardDirs::NoDuplicates
, fileList
);
121 foreach ( const QString
& fileName
, fileList
)
123 Ruleset
* ruleset
= Ruleset::load( fileName
);
126 QString name
= ruleset
->name();
127 while ( m_rulesetMap
.contains( name
) )
130 m_rulesetMap
.insert( name
, ruleset
);
132 QListWidgetItem
* item
= new QListWidgetItem( name
, m_listWidget
);
133 if ( fileName
== Settings::ruleset() )
134 m_listWidget
->setCurrentItem( item
);
140 // Set the maximum height of the list widget to be no more than the size of its contents
141 // This is slightly hackish, but the effect is nice.
142 const int itemHeight
= m_listWidget
->visualItemRect( m_listWidget
->item( 0 ) ).height();
143 const int verticalMargin
= m_listWidget
->height() - m_listWidget
->viewport()->height();
144 m_listWidget
->setMaximumHeight( itemHeight
* m_listWidget
->count() + verticalMargin
);
148 void Killbots::RulesetSelector::selectionChanged( QString rulesetName
)
150 Ruleset
* ruleset
= m_rulesetMap
.value( rulesetName
);
152 kcfg_Ruleset
->setText( ruleset
->fileName() );
154 m_author
->setText( ruleset
->author() );
155 if ( ruleset
->authorContact().contains(' ') )
156 m_authorContact
->setText( ruleset
->authorContact() );
157 else if ( ruleset
->authorContact().contains('@') )
158 m_authorContact
->setText( QString("<qt><a href=\"mailto:%1\">%1</a></qt>").arg( ruleset
->authorContact() ) );
159 else if ( ruleset
->authorContact().contains('.') )
160 m_authorContact
->setText( QString("<qt><a href=\"http://%1\">%1</a></qt>").arg( ruleset
->authorContact() ) );
162 m_authorContact
->setText( ruleset
->authorContact() );
163 m_description
->setText( ruleset
->description() );
165 if ( m_detailsDialog
&& m_detailsDialog
->isVisible() )
166 m_detailsDialog
->loadRuleset( ruleset
);
170 void Killbots::RulesetSelector::showDetailsDialog()
172 if ( !m_detailsDialog
)
173 m_detailsDialog
= new RulesetDetailsDialog( this );
175 Ruleset
* ruleset
= m_rulesetMap
.value( m_listWidget
->currentItem()->text() );
176 m_detailsDialog
->loadRuleset( ruleset
);
177 m_detailsDialog
->show();