1 | /******************************************************************* |
2 | * |
3 | * Copyright 2006-2009 Dmitry Suzdalev <dimsuz@gmail.com> |
4 | * |
5 | * This file is part of the KDE project "KAtomic" |
6 | * |
7 | * KAtomic is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2, or (at your option) |
10 | * any later version. |
11 | * |
12 | * KAtomic is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with KAtomic; see the file COPYING. If not, write to |
19 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | * |
22 | ********************************************************************/ |
23 | #include "chooselevelsetdialog.h" |
24 | |
25 | #include <KStandardDirs> |
26 | #include <KDebug> |
27 | #include <KNS3/KNewStuffButton> |
28 | |
29 | #include "levelset.h" |
30 | #include "levelsetdelegate.h" |
31 | |
32 | #include "commondefs.h" |
33 | |
34 | ChooseLevelSetDialog::ChooseLevelSetDialog(QWidget* parent) |
35 | : KDialog(parent) |
36 | { |
37 | setAttribute(Qt::WA_DeleteOnClose); |
38 | |
39 | setCaption(i18n("Level Sets" )); |
40 | setButtons(KDialog::Ok | KDialog::Apply | KDialog::Cancel); |
41 | |
42 | QWidget* chooseWidget = new QWidget(this); |
43 | m_ui.setupUi(chooseWidget); |
44 | |
45 | m_ui.m_lwLevelSets->setItemDelegate(new LevelSetDelegate(this)); |
46 | m_ui.m_lwLevelSets->setSortingEnabled(true); |
47 | |
48 | m_ui.m_pbNewStuff->setConfigFile("katomic.knsrc" ); |
49 | |
50 | setMainWidget(chooseWidget); |
51 | |
52 | resize(550, 350); |
53 | |
54 | loadData(); |
55 | |
56 | connect(m_ui.m_lwLevelSets, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), |
57 | SLOT(updateApplyButton())); |
58 | connect(m_ui.m_pbNewStuff, SIGNAL(dialogFinished(KNS3::Entry::List)), SLOT(newStuffDone(KNS3::Entry::List))); |
59 | } |
60 | |
61 | void ChooseLevelSetDialog::newStuffDone(const KNS3::Entry::List& entries) |
62 | { |
63 | if (!entries.isEmpty()) |
64 | loadData(); |
65 | } |
66 | |
67 | void ChooseLevelSetDialog::loadData() |
68 | { |
69 | m_ui.m_lwLevelSets->clear(); |
70 | |
71 | QStringList fileList = KGlobal::dirs()->findAllResources("appdata" , "levels/*.dat" , KStandardDirs::NoDuplicates); |
72 | |
73 | LevelSet ls; |
74 | foreach (const QString& fileName, fileList) |
75 | { |
76 | ls.loadFromFile(fileName); |
77 | QString visibleName = ls.visibleName(); |
78 | if (!visibleName.isEmpty()) |
79 | { |
80 | QListWidgetItem* item = new QListWidgetItem; |
81 | item->setIcon(KIcon( QLatin1String( "katomic" ))); |
82 | item->setText(visibleName); |
83 | item->setData(KAtomic::LevelSetNameRole, ls.name()); |
84 | item->setData(KAtomic::LevelSetDescriptionRole, ls.description()); |
85 | item->setData(KAtomic::LevelSetAuthorRole, ls.author()); |
86 | item->setData(KAtomic::LevelSetAuthorEmailRole, ls.authorEmail()); |
87 | item->setData(KAtomic::LevelSetLevelCountRole, ls.levelCount()); |
88 | m_ui.m_lwLevelSets->addItem(item); |
89 | } |
90 | } |
91 | |
92 | // reselect current levelset if it is set |
93 | if(!m_gameCurrentLevelSetName.isEmpty()) |
94 | setCurrentLevelSet(m_gameCurrentLevelSetName); |
95 | } |
96 | |
97 | void ChooseLevelSetDialog::setCurrentLevelSet(const QString& levelSetName) |
98 | { |
99 | m_gameCurrentLevelSetName = levelSetName; |
100 | |
101 | int count = m_ui.m_lwLevelSets->count(); |
102 | for (int i=0;i<count;++i) |
103 | { |
104 | QListWidgetItem* item = m_ui.m_lwLevelSets->item(i); |
105 | if (!item) |
106 | continue; |
107 | |
108 | if (item->data(KAtomic::LevelSetNameRole).toString() == levelSetName) |
109 | { |
110 | m_ui.m_lwLevelSets->setCurrentItem(item); |
111 | break; |
112 | } |
113 | } |
114 | } |
115 | |
116 | void ChooseLevelSetDialog::slotButtonClicked(int but) |
117 | { |
118 | if (but == KDialog::Ok || but == KDialog::Apply) |
119 | { |
120 | QListWidgetItem* item = m_ui.m_lwLevelSets->currentItem(); |
121 | if (item) |
122 | { |
123 | QString levelSetName = item->data(KAtomic::LevelSetNameRole).toString(); |
124 | emit levelSetChanged(levelSetName); |
125 | |
126 | m_gameCurrentLevelSetName = levelSetName; |
127 | updateApplyButton(); |
128 | } |
129 | } |
130 | |
131 | KDialog::slotButtonClicked(but); |
132 | } |
133 | |
134 | void ChooseLevelSetDialog::updateApplyButton() |
135 | { |
136 | QListWidgetItem* item = m_ui.m_lwLevelSets->currentItem(); |
137 | if (item) |
138 | { |
139 | enableButtonApply(item->data(KAtomic::LevelSetNameRole).toString() != m_gameCurrentLevelSetName); |
140 | } |
141 | } |
142 | |