1 | /* |
2 | * Copyright 2007-2009 Parker Coates <coates@kde.org> |
3 | * |
4 | * This file is part of Killbots. |
5 | * |
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. |
10 | * |
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. |
15 | * |
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/>. |
18 | */ |
19 | |
20 | #include "rulesetselector.h" |
21 | |
22 | #include "ruleset.h" |
23 | #include "rulesetdetailsdialog.h" |
24 | #include "settings.h" |
25 | |
26 | #include <KDE/KDebug> |
27 | #include <KDE/KLineEdit> |
28 | #include <KDE/KLocalizedString> |
29 | #include <KDE/KStandardDirs> |
30 | |
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> |
38 | |
39 | Killbots::RulesetSelector::RulesetSelector( QWidget * parent ) |
40 | : QWidget( parent ), |
41 | m_rulesetMap(), |
42 | m_detailsDialog( 0 ) |
43 | { |
44 | // Create a hidden KLineEdit to use the automatic KConfigXT connection |
45 | kcfg_Ruleset = new KLineEdit(); |
46 | kcfg_Ruleset->setObjectName( QLatin1String( "kcfg_Ruleset" ) ); |
47 | kcfg_Ruleset->hide(); |
48 | |
49 | m_listWidget = new QListWidget(); |
50 | m_listWidget->setWhatsThis( i18n("A list of the Killbots rulesets installed on this computer." ) ); |
51 | |
52 | QGroupBox * groupBox = new QGroupBox( i18n("Game Type Details" ) ); |
53 | groupBox->setWhatsThis( i18n("Lists information on the currently selected game type." ) ); |
54 | |
55 | QLabel * authorLabel = new QLabel( i18n("Author:" ) ); |
56 | authorLabel->setAlignment( Qt::AlignRight | Qt::AlignTop ); |
57 | |
58 | m_author = new QLabel(); |
59 | m_author->setAlignment( Qt::AlignLeft | Qt::AlignTop ); |
60 | m_author->setWordWrap( true ); |
61 | |
62 | QLabel * contactLabel = new QLabel( i18n("Contact:" ) ); |
63 | contactLabel->setAlignment( Qt::AlignRight | Qt::AlignTop ); |
64 | |
65 | m_authorContact = new QLabel(); |
66 | m_authorContact->setAlignment( Qt::AlignLeft | Qt::AlignTop ); |
67 | m_authorContact->setWordWrap( true ); |
68 | m_authorContact->setOpenExternalLinks ( true ); |
69 | |
70 | QLabel * descriptionLabel = new QLabel( i18n("Description:" ) ); |
71 | descriptionLabel->setAlignment( Qt::AlignRight | Qt::AlignTop ); |
72 | |
73 | m_description = new QLabel(); |
74 | m_description->setAlignment( Qt::AlignLeft | Qt::AlignTop ); |
75 | m_description->setWordWrap( true ); |
76 | |
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." ) ); |
80 | |
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 ); |
91 | |
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 ); |
97 | |
98 | connect( m_listWidget, SIGNAL(currentTextChanged(QString)), this, SLOT(selectionChanged(QString)) ); |
99 | connect( detailsButton, SIGNAL(clicked()), this, SLOT(showDetailsDialog()) ); |
100 | |
101 | findRulesets(); |
102 | } |
103 | |
104 | |
105 | Killbots::RulesetSelector::~RulesetSelector() |
106 | { |
107 | qDeleteAll( m_rulesetMap ); |
108 | } |
109 | |
110 | |
111 | void Killbots::RulesetSelector::findRulesets() |
112 | { |
113 | qDeleteAll( m_rulesetMap ); |
114 | m_rulesetMap.clear(); |
115 | |
116 | m_listWidget->clear(); |
117 | m_listWidget->setSortingEnabled( true ); |
118 | |
119 | QStringList fileList; |
120 | KGlobal::dirs()->findAllResources ( "ruleset" , "*.desktop" , KStandardDirs::NoDuplicates, fileList ); |
121 | foreach ( const QString & fileName, fileList ) |
122 | { |
123 | const Ruleset * ruleset = Ruleset::load( fileName ); |
124 | if ( ruleset ) |
125 | { |
126 | QString name = ruleset->name(); |
127 | while ( m_rulesetMap.contains( name ) ) |
128 | name += '_'; |
129 | |
130 | m_rulesetMap.insert( name, ruleset ); |
131 | |
132 | QListWidgetItem * item = new QListWidgetItem( name, m_listWidget ); |
133 | if ( fileName == Settings::ruleset() ) |
134 | m_listWidget->setCurrentItem( item ); |
135 | } |
136 | else |
137 | delete ruleset; |
138 | } |
139 | |
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 ); |
145 | } |
146 | |
147 | |
148 | void Killbots::RulesetSelector::selectionChanged( QString rulesetName ) |
149 | { |
150 | const Ruleset * ruleset = m_rulesetMap.value( rulesetName ); |
151 | |
152 | kcfg_Ruleset->setText( ruleset->fileName() ); |
153 | |
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() ) ); |
161 | else |
162 | m_authorContact->setText( ruleset->authorContact() ); |
163 | m_description->setText( ruleset->description() ); |
164 | |
165 | if ( m_detailsDialog && m_detailsDialog->isVisible() ) |
166 | m_detailsDialog->loadRuleset( ruleset ); |
167 | } |
168 | |
169 | |
170 | void Killbots::RulesetSelector::showDetailsDialog() |
171 | { |
172 | if ( !m_detailsDialog ) |
173 | m_detailsDialog = new RulesetDetailsDialog( this ); |
174 | |
175 | const Ruleset * ruleset = m_rulesetMap.value( m_listWidget->currentItem()->text() ); |
176 | m_detailsDialog->loadRuleset( ruleset ); |
177 | m_detailsDialog->show(); |
178 | } |
179 | |
180 | |