~daggerstab/stellarium/add-remove-landscapes

« back to all changes in this revision

Viewing changes to src/gui/AddRemoveLandscapesDialog.cpp

  • Committer: Bogdan Marinov
  • Date: 2010-09-05 12:28:04 UTC
  • Revision ID: bogdan.marinov84@gmail.com-20100905122804-y935vv39g4k8vibd
merged the AddRemoveLandscapes plug-in into LandscapeMgr and ViewDialog; added some minor features and bug fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * 
 
4
 * Copyright (C) 2010 Bogdan Marinov (add/remove landscapes feature)
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 * 
 
11
 * This program 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 this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
*/
 
20
#include "AddRemoveLandscapesDialog.hpp"
 
21
#include "ui_addRemoveLandscapesDialog.h"
 
22
 
 
23
#include "Dialog.hpp"
 
24
#include "LandscapeMgr.hpp"
 
25
#include "StelApp.hpp"
 
26
#include "StelModuleMgr.hpp"
 
27
#include "StelLocaleMgr.hpp"
 
28
 
 
29
#include <QDebug>
 
30
#include <QFileDialog>
 
31
#include <QString>
 
32
 
 
33
AddRemoveLandscapesDialog::AddRemoveLandscapesDialog()
 
34
{
 
35
        ui = new Ui_addRemoveLandscapesDialogForm;
 
36
        
 
37
        landscapeManager = GETSTELMODULE(LandscapeMgr);
 
38
 
 
39
        lastUsedDirectoryPath = QDir::homePath();
 
40
 
 
41
        //TODO: Find a way to have this initialized from CMake
 
42
        defaultLandscapeIDs = (QStringList() << "guereins" << "trees" << "moon" << "hurricane" << "ocean" << "garching" << "mars" << "saturn");
 
43
}
 
44
 
 
45
AddRemoveLandscapesDialog::~AddRemoveLandscapesDialog()
 
46
{
 
47
        delete ui;
 
48
}
 
49
 
 
50
void AddRemoveLandscapesDialog::languageChanged()
 
51
{
 
52
        if (dialog)
 
53
                ui->retranslateUi(dialog);
 
54
}
 
55
 
 
56
// Initialize the dialog widgets and connect the signals/slots
 
57
void AddRemoveLandscapesDialog::createDialogContent()
 
58
{
 
59
        ui->setupUi(dialog);
 
60
        
 
61
        //Connect all signals and slots here: sender, signal, receiver, method
 
62
        connect(ui->closeStelWindow, SIGNAL(clicked()), this, SLOT(close()));
 
63
 
 
64
        connect(ui->pushButtonAdd, SIGNAL(clicked()), this, SLOT(buttonAddClicked()));
 
65
        connect(ui->listWidgetUserLandscapes, SIGNAL(currentRowChanged(int)), this, SLOT(landscapeListCurrentRowChanged(int)));
 
66
        connect(ui->pushButtonRemove, SIGNAL(clicked()), this, SLOT(buttonRemoveClicked()));
 
67
        connect(ui->pushButtonMessageOK, SIGNAL(clicked()), this, SLOT(messageAcknowledged()));
 
68
 
 
69
        connect(landscapeManager, SIGNAL(landscapesChanged()), this, SLOT(populateLists()));
 
70
 
 
71
        ui->groupBoxMessage->setVisible(false);
 
72
 
 
73
        populateLists();
 
74
}
 
75
 
 
76
void AddRemoveLandscapesDialog::populateLists()
 
77
{
 
78
        ui->listWidgetUserLandscapes->clear();
 
79
        QStringList landscapes = landscapeManager->getAllLandscapeIDs();
 
80
        foreach (QString landscapeID, defaultLandscapeIDs)
 
81
        {
 
82
                landscapes.removeAll(landscapeID);
 
83
        }
 
84
        if (!landscapes.isEmpty())
 
85
        {
 
86
                landscapes.sort();
 
87
                ui->listWidgetUserLandscapes->addItems(landscapes);
 
88
                if((ui->listWidgetUserLandscapes->findItems(landscapeManager->getCurrentLandscapeID(), Qt::MatchExactly).isEmpty()))
 
89
                {
 
90
                        //If the current landscape is not in the list, simply select the first row
 
91
                        ui->listWidgetUserLandscapes->setCurrentRow(0);
 
92
                }
 
93
                else
 
94
                {
 
95
                        ui->listWidgetUserLandscapes->setCurrentItem(ui->listWidgetUserLandscapes->findItems(landscapeManager->getCurrentLandscapeID(), Qt::MatchExactly).first());
 
96
                }
 
97
        }
 
98
        else
 
99
        {
 
100
                //Force disabling the side pane
 
101
                landscapeListCurrentRowChanged(-1);
 
102
        }
 
103
}
 
104
 
 
105
void AddRemoveLandscapesDialog::buttonAddClicked()
 
106
{
 
107
        QString sourceArchivePath = QFileDialog::getOpenFileName(NULL, "Select a ZIP archive containing a Stellarium landscape...", lastUsedDirectoryPath, "ZIP archives (*.zip)");
 
108
        bool useLandscape = ui->checkBoxUseLandscape->isChecked();
 
109
        if (!sourceArchivePath.isEmpty() && QFile::exists(sourceArchivePath))
 
110
        {
 
111
                QString newLandscapeID = landscapeManager->installLandscapeFromArchive(sourceArchivePath, useLandscape);
 
112
                if(!newLandscapeID.isEmpty())
 
113
                {
 
114
                        //Show a message
 
115
                        displayMessage("Add", "Landscape installed successfully.");
 
116
                        ui->groupBoxAdd->setVisible(false);
 
117
                        //Make the new landscape selected in the list
 
118
                        //populateLists(); //No longer needed after the migration to signals/slots
 
119
                        ui->listWidgetUserLandscapes->setCurrentItem((ui->listWidgetUserLandscapes->findItems(newLandscapeID, Qt::MatchExactly)).first());
 
120
                }
 
121
                else
 
122
                {
 
123
                        //Show an error message
 
124
                        displayMessage("Error!", "Landscape was not installed.");
 
125
                        ui->groupBoxAdd->setVisible(false);
 
126
                }
 
127
        }
 
128
}
 
129
 
 
130
void AddRemoveLandscapesDialog::buttonRemoveClicked()
 
131
{
 
132
        QString landscapeID = ui->listWidgetUserLandscapes->currentItem()->data(0).toString();
 
133
        if(landscapeManager->removeLandscape(landscapeID))
 
134
        {
 
135
                //populateLists();//No longer needed after the migration to signals/slots
 
136
                //TODO: Display messages instead
 
137
        }
 
138
 
 
139
}
 
140
 
 
141
void AddRemoveLandscapesDialog::landscapeListCurrentRowChanged(int newRow)
 
142
{
 
143
        bool displaySidePane = (newRow >= 0);
 
144
        ui->labelLandscapeName->setVisible(displaySidePane);
 
145
        ui->labelLandscapeSize->setVisible(displaySidePane);
 
146
        ui->pushButtonRemove->setEnabled(displaySidePane);
 
147
        ui->labelWarning->setEnabled(displaySidePane);
 
148
 
 
149
        if (!displaySidePane)
 
150
                return;
 
151
 
 
152
        QString landscapeID = ui->listWidgetUserLandscapes->item(newRow)->data(0).toString();
 
153
        //Name
 
154
        ui->labelLandscapeName->setText("<h3>"+landscapeManager->loadLandscapeName(landscapeID)+"</h3>");
 
155
        //Size in MiB
 
156
        double landscapeSize = landscapeManager->loadLandscapeSize(landscapeID) / (double)(1024*1024);
 
157
        ui->labelLandscapeSize->setText(QString("Size on disk: %1 MiB").arg(landscapeSize, 0, 'f', 2));
 
158
}
 
159
 
 
160
void AddRemoveLandscapesDialog::messageAcknowledged()
 
161
{
 
162
        ui->groupBoxMessage->setVisible(false);
 
163
        ui->groupBoxAdd->setVisible(true);
 
164
        ui->labelMessage->clear();
 
165
}
 
166
 
 
167
void AddRemoveLandscapesDialog::displayMessage(QString title, QString message)
 
168
{
 
169
        ui->labelMessage->setText(message);
 
170
        ui->groupBoxMessage->setTitle(title);
 
171
        ui->groupBoxMessage->setVisible(true);
 
172
}