summaryrefslogtreecommitdiffstats
path: root/Source/QtDialog/CMakeSetupDialog.cxx
blob: d54c7cfd51b2c31350fb5622e9dcefedfdf108a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "CMakeSetupDialog.h"

#include <QFileDialog>
#include <QThread>
#include <QProgressBar>
#include <QMessageBox>

#include "QCMake.h"
#include "QCMakeCacheView.h"

// QCMake instance on a thread
class QCMakeThread : public QThread
{
public:
  QCMakeThread(QObject* p) : QThread(p) { }
  QCMake* CMakeInstance;

protected:
  virtual void run()
  {
    this->CMakeInstance = new QCMake;
    this->exec();
    delete this->CMakeInstance;
  }
};

CMakeSetupDialog::CMakeSetupDialog()
{
  // create the GUI
  this->setupUi(this);
  this->ProgressBar = new QProgressBar();
  this->ProgressBar->setRange(0,100);
  this->statusBar()->addPermanentWidget(this->ProgressBar);
  
  // start the cmake worker thread
  this->CMakeThread = new QCMakeThread(this);
  // TODO does this guarantee the QCMake instance is created before initialize is called?
  QObject::connect(this->CMakeThread, SIGNAL(started()),
                   this, SLOT(initialize()));  
  this->CMakeThread->start();
}

void CMakeSetupDialog::initialize()
{
  // now the cmake worker thread is running, lets make our connections to it
  QObject::connect(this->CMakeThread->CMakeInstance, 
      SIGNAL(propertiesChanged(const QCMakeCachePropertyList&)),
      this->CacheValues->cacheModel(),
      SLOT(setProperties(const QCMakeCachePropertyList&)));
  QObject::connect(this,
      SIGNAL(propertiesChanged(const QCMakeCachePropertyList&)),
      this->CMakeThread->CMakeInstance,
      SLOT(setProperties(const QCMakeCachePropertyList&)));

  QObject::connect(this->configureButton, SIGNAL(clicked(bool)),
                   this, SLOT(doConfigure()));
  QObject::connect(this, SIGNAL(configure()),
                   this->CMakeThread->CMakeInstance, SLOT(configure()));
  QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(configureDone(int)),
                   this, SLOT(finishConfigure(int)));
  QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(generateDone(int)),
                   this, SLOT(finishGenerate(int)));

  QObject::connect(this->generateButton, SIGNAL(clicked(bool)),
                   this, SLOT(doOk()));
  QObject::connect(this, SIGNAL(ok()),
                   this->CMakeThread->CMakeInstance, SLOT(generate()));
  
  QObject::connect(this->cancelButton, SIGNAL(clicked(bool)),
                   this, SLOT(doCancel()));
  QObject::connect(this, SIGNAL(cancel()),
                   this->CMakeThread->CMakeInstance, SLOT(interrupt()));
  
  QObject::connect(this->BrowseSourceDirectoryButton, SIGNAL(clicked(bool)),
                   this, SLOT(doSourceBrowse()));
  QObject::connect(this->BrowseBinaryDirectoryButton, SIGNAL(clicked(bool)),
                   this, SLOT(doBinaryBrowse()));
  
  QObject::connect(this->BinaryDirectory, SIGNAL(textChanged(QString)),
                   this->CMakeThread->CMakeInstance, SLOT(setBinaryDirectory(QString)));

  QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(sourceDirChanged(QString)),
                   this, SLOT(updateSourceDirectory(QString)));
 
  QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(progressChanged(QString, float)),
                   this, SLOT(showProgress(QString,float)));
  
  QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(error(QString, QString, bool*)),
                   this, SLOT(error(QString,QString,bool*)), Qt::BlockingQueuedConnection);

}

CMakeSetupDialog::~CMakeSetupDialog()
{
  // wait for thread to stop
  this->CMakeThread->quit();
  this->CMakeThread->wait();
}
  
void CMakeSetupDialog::doConfigure()
{
  emit this->propertiesChanged(this->CacheValues->cacheModel()->properties());
  emit this->configure();
}

void CMakeSetupDialog::finishConfigure(int error)
{
  this->ProgressBar->reset();
  this->statusBar()->showMessage("Configure Done", 2000);
  if(error != 0)
  {
    bool dummy;
    this->error("Error", "Error in configuration process, project files may be invalid", &dummy);
  }
}

void CMakeSetupDialog::finishGenerate(int error)
{
  this->ProgressBar->reset();
  this->statusBar()->showMessage("Generate Done", 2000);
  if(error != 0)
  {
    bool dummy;
    this->error("Error", "Error in generation process, project files may be invalid", &dummy);
  }
}

void CMakeSetupDialog::doOk()
{
  emit this->ok();
}

void CMakeSetupDialog::doCancel()
{
  emit this->cancel();
}

void CMakeSetupDialog::doHelp()
{
}

void CMakeSetupDialog::doSourceBrowse()
{
  QString dir = QFileDialog::getExistingDirectory(this, "TODO", this->SourceDirectory->text());
  if(!dir.isEmpty())
    {
    this->updateSourceDirectory(dir);
    }
}

void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
{
  this->SourceDirectory->setText(dir);
}

void CMakeSetupDialog::doBinaryBrowse()
{
  QString dir = QFileDialog::getExistingDirectory(this, "TODO", this->BinaryDirectory->currentText());
  if(!dir.isEmpty())
    {
    this->setBinaryDirectory(dir);
    }
}

void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
{
  if(dir != this->BinaryDirectory->currentText())
  {
    this->BinaryDirectory->setEditText(dir);
  }
}

void CMakeSetupDialog::showProgress(const QString& msg, float percent)
{
  if(percent >= 0)
  {
    this->statusBar()->showMessage(msg);
    this->ProgressBar->setValue(qRound(percent * 100));
  }
}
  
void CMakeSetupDialog::error(const QString& title, const QString& message, bool* cancel)
{
  QMessageBox::StandardButton btn =
    QMessageBox::critical(this, title, message, QMessageBox::Ok | QMessageBox::Cancel);
  if(btn == QMessageBox::Cancel)
  {
    *cancel = false;
  }
}