CMakeSetupDialog.cxx 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "CMakeSetupDialog.h"
  11. #include <QFileDialog>
  12. #include <QProgressBar>
  13. #include <QMessageBox>
  14. #include <QStatusBar>
  15. #include <QToolButton>
  16. #include <QDialogButtonBox>
  17. #include <QCloseEvent>
  18. #include <QCoreApplication>
  19. #include <QSettings>
  20. #include <QMenu>
  21. #include <QMenuBar>
  22. #include <QDragEnterEvent>
  23. #include <QMimeData>
  24. #include <QUrl>
  25. #include <QShortcut>
  26. #include <QMacInstallDialog.h>
  27. #include "QCMake.h"
  28. #include "QCMakeCacheView.h"
  29. #include "AddCacheEntry.h"
  30. #include "FirstConfigure.h"
  31. #include "cmVersion.h"
  32. QCMakeThread::QCMakeThread(QObject* p)
  33. : QThread(p), CMakeInstance(NULL)
  34. {
  35. }
  36. QCMake* QCMakeThread::cmakeInstance() const
  37. {
  38. return this->CMakeInstance;
  39. }
  40. void QCMakeThread::run()
  41. {
  42. this->CMakeInstance = new QCMake;
  43. // emit that this cmake thread is ready for use
  44. emit this->cmakeInitialized();
  45. this->exec();
  46. delete this->CMakeInstance;
  47. this->CMakeInstance = NULL;
  48. }
  49. CMakeSetupDialog::CMakeSetupDialog()
  50. : ExitAfterGenerate(true), CacheModified(false), CurrentState(Interrupting)
  51. {
  52. QString title = QString(tr("CMake %1"));
  53. title = title.arg(cmVersion::GetCMakeVersion());
  54. this->setWindowTitle(title);
  55. // create the GUI
  56. QSettings settings;
  57. settings.beginGroup("Settings/StartPath");
  58. int h = settings.value("Height", 500).toInt();
  59. int w = settings.value("Width", 700).toInt();
  60. this->resize(w, h);
  61. QWidget* cont = new QWidget(this);
  62. this->setupUi(cont);
  63. this->Splitter->setStretchFactor(0, 3);
  64. this->Splitter->setStretchFactor(1, 1);
  65. this->setCentralWidget(cont);
  66. this->ProgressBar->reset();
  67. this->RemoveEntry->setEnabled(false);
  68. this->AddEntry->setEnabled(false);
  69. QByteArray p = settings.value("SplitterSizes").toByteArray();
  70. this->Splitter->restoreState(p);
  71. bool groupView = settings.value("GroupView", false).toBool();
  72. this->setGroupedView(groupView);
  73. this->groupedCheck->setCheckState(groupView ? Qt::Checked : Qt::Unchecked);
  74. QMenu* FileMenu = this->menuBar()->addMenu(tr("&File"));
  75. this->ReloadCacheAction = FileMenu->addAction(tr("&Reload Cache"));
  76. QObject::connect(this->ReloadCacheAction, SIGNAL(triggered(bool)),
  77. this, SLOT(doReloadCache()));
  78. this->DeleteCacheAction = FileMenu->addAction(tr("&Delete Cache"));
  79. QObject::connect(this->DeleteCacheAction, SIGNAL(triggered(bool)),
  80. this, SLOT(doDeleteCache()));
  81. this->ExitAction = FileMenu->addAction(tr("E&xit"));
  82. QObject::connect(this->ExitAction, SIGNAL(triggered(bool)),
  83. this, SLOT(close()));
  84. QMenu* ToolsMenu = this->menuBar()->addMenu(tr("&Tools"));
  85. this->ConfigureAction = ToolsMenu->addAction(tr("&Configure"));
  86. // prevent merging with Preferences menu item on Mac OS X
  87. this->ConfigureAction->setMenuRole(QAction::NoRole);
  88. QObject::connect(this->ConfigureAction, SIGNAL(triggered(bool)),
  89. this, SLOT(doConfigure()));
  90. this->GenerateAction = ToolsMenu->addAction(tr("&Generate"));
  91. QObject::connect(this->GenerateAction, SIGNAL(triggered(bool)),
  92. this, SLOT(doGenerate()));
  93. QAction* showChangesAction = ToolsMenu->addAction(tr("&Show My Changes"));
  94. QObject::connect(showChangesAction, SIGNAL(triggered(bool)),
  95. this, SLOT(showUserChanges()));
  96. #if defined(Q_WS_MAC)
  97. this->InstallForCommandLineAction
  98. = ToolsMenu->addAction(tr("&Install For Command Line Use"));
  99. QObject::connect(this->InstallForCommandLineAction, SIGNAL(triggered(bool)),
  100. this, SLOT(doInstallForCommandLine()));
  101. #endif
  102. QMenu* OptionsMenu = this->menuBar()->addMenu(tr("&Options"));
  103. this->SuppressDevWarningsAction =
  104. OptionsMenu->addAction(tr("&Suppress dev Warnings (-Wno-dev)"));
  105. this->SuppressDevWarningsAction->setCheckable(true);
  106. this->WarnUninitializedAction =
  107. OptionsMenu->addAction(tr("&Warn Uninitialized (--warn-uninitialized)"));
  108. this->WarnUninitializedAction->setCheckable(true);
  109. QAction* debugAction = OptionsMenu->addAction(tr("&Debug Output"));
  110. debugAction->setCheckable(true);
  111. QObject::connect(debugAction, SIGNAL(toggled(bool)),
  112. this, SLOT(setDebugOutput(bool)));
  113. OptionsMenu->addSeparator();
  114. QAction* expandAction = OptionsMenu->addAction(tr("&Expand Grouped Entries"));
  115. QObject::connect(expandAction, SIGNAL(triggered(bool)),
  116. this->CacheValues, SLOT(expandAll()));
  117. QAction* collapseAction = OptionsMenu->addAction(tr("&Collapse Grouped Entries"));
  118. QObject::connect(collapseAction, SIGNAL(triggered(bool)),
  119. this->CacheValues, SLOT(collapseAll()));
  120. QMenu* HelpMenu = this->menuBar()->addMenu(tr("&Help"));
  121. QAction* a = HelpMenu->addAction(tr("About"));
  122. QObject::connect(a, SIGNAL(triggered(bool)),
  123. this, SLOT(doAbout()));
  124. a = HelpMenu->addAction(tr("Help"));
  125. QObject::connect(a, SIGNAL(triggered(bool)),
  126. this, SLOT(doHelp()));
  127. QShortcut* filterShortcut = new QShortcut(QKeySequence::Find, this);
  128. QObject::connect(filterShortcut, SIGNAL(activated()),
  129. this, SLOT(startSearch()));
  130. this->setAcceptDrops(true);
  131. // get the saved binary directories
  132. QStringList buildPaths = this->loadBuildPaths();
  133. this->BinaryDirectory->addItems(buildPaths);
  134. this->BinaryDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  135. this->SourceDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  136. // fixed pitch font in output window
  137. QFont outputFont("Courier");
  138. this->Output->setFont(outputFont);
  139. this->ErrorFormat.setForeground(QBrush(Qt::red));
  140. // start the cmake worker thread
  141. this->CMakeThread = new QCMakeThread(this);
  142. QObject::connect(this->CMakeThread, SIGNAL(cmakeInitialized()),
  143. this, SLOT(initialize()), Qt::QueuedConnection);
  144. this->CMakeThread->start();
  145. this->enterState(ReadyConfigure);
  146. }
  147. void CMakeSetupDialog::initialize()
  148. {
  149. // now the cmake worker thread is running, lets make our connections to it
  150. QObject::connect(this->CMakeThread->cmakeInstance(),
  151. SIGNAL(propertiesChanged(const QCMakePropertyList&)),
  152. this->CacheValues->cacheModel(),
  153. SLOT(setProperties(const QCMakePropertyList&)));
  154. QObject::connect(this->ConfigureButton, SIGNAL(clicked(bool)),
  155. this, SLOT(doConfigure()));
  156. QObject::connect(this->CMakeThread->cmakeInstance(),
  157. SIGNAL(configureDone(int)),
  158. this, SLOT(finishConfigure(int)));
  159. QObject::connect(this->CMakeThread->cmakeInstance(),
  160. SIGNAL(generateDone(int)),
  161. this, SLOT(finishGenerate(int)));
  162. QObject::connect(this->GenerateButton, SIGNAL(clicked(bool)),
  163. this, SLOT(doGenerate()));
  164. QObject::connect(this->BrowseSourceDirectoryButton, SIGNAL(clicked(bool)),
  165. this, SLOT(doSourceBrowse()));
  166. QObject::connect(this->BrowseBinaryDirectoryButton, SIGNAL(clicked(bool)),
  167. this, SLOT(doBinaryBrowse()));
  168. QObject::connect(this->BinaryDirectory, SIGNAL(editTextChanged(QString)),
  169. this, SLOT(onBinaryDirectoryChanged(QString)));
  170. QObject::connect(this->SourceDirectory, SIGNAL(textChanged(QString)),
  171. this, SLOT(onSourceDirectoryChanged(QString)));
  172. QObject::connect(this->CMakeThread->cmakeInstance(),
  173. SIGNAL(sourceDirChanged(QString)),
  174. this, SLOT(updateSourceDirectory(QString)));
  175. QObject::connect(this->CMakeThread->cmakeInstance(),
  176. SIGNAL(binaryDirChanged(QString)),
  177. this, SLOT(updateBinaryDirectory(QString)));
  178. QObject::connect(this->CMakeThread->cmakeInstance(),
  179. SIGNAL(progressChanged(QString, float)),
  180. this, SLOT(showProgress(QString,float)));
  181. QObject::connect(this->CMakeThread->cmakeInstance(),
  182. SIGNAL(errorMessage(QString)),
  183. this, SLOT(error(QString)));
  184. QObject::connect(this->CMakeThread->cmakeInstance(),
  185. SIGNAL(outputMessage(QString)),
  186. this, SLOT(message(QString)));
  187. QObject::connect(this->groupedCheck, SIGNAL(toggled(bool)),
  188. this, SLOT(setGroupedView(bool)));
  189. QObject::connect(this->advancedCheck, SIGNAL(toggled(bool)),
  190. this, SLOT(setAdvancedView(bool)));
  191. QObject::connect(this->Search, SIGNAL(textChanged(QString)),
  192. this, SLOT(setSearchFilter(QString)));
  193. QObject::connect(this->CMakeThread->cmakeInstance(),
  194. SIGNAL(generatorChanged(QString)),
  195. this, SLOT(updateGeneratorLabel(QString)));
  196. this->updateGeneratorLabel(QString());
  197. QObject::connect(this->CacheValues->cacheModel(),
  198. SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  199. this, SLOT(setCacheModified()));
  200. QObject::connect(this->CacheValues->selectionModel(),
  201. SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  202. this, SLOT(selectionChanged()));
  203. QObject::connect(this->RemoveEntry, SIGNAL(clicked(bool)),
  204. this, SLOT(removeSelectedCacheEntries()));
  205. QObject::connect(this->AddEntry, SIGNAL(clicked(bool)),
  206. this, SLOT(addCacheEntry()));
  207. QObject::connect(this->SuppressDevWarningsAction, SIGNAL(triggered(bool)),
  208. this->CMakeThread->cmakeInstance(), SLOT(setSuppressDevWarnings(bool)));
  209. QObject::connect(this->WarnUninitializedAction, SIGNAL(triggered(bool)),
  210. this->CMakeThread->cmakeInstance(),
  211. SLOT(setWarnUninitializedMode(bool)));
  212. if(!this->SourceDirectory->text().isEmpty() ||
  213. !this->BinaryDirectory->lineEdit()->text().isEmpty())
  214. {
  215. this->onSourceDirectoryChanged(this->SourceDirectory->text());
  216. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  217. }
  218. else
  219. {
  220. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  221. }
  222. }
  223. CMakeSetupDialog::~CMakeSetupDialog()
  224. {
  225. QSettings settings;
  226. settings.beginGroup("Settings/StartPath");
  227. settings.setValue("Height", this->height());
  228. settings.setValue("Width", this->width());
  229. settings.setValue("SplitterSizes", this->Splitter->saveState());
  230. // wait for thread to stop
  231. this->CMakeThread->quit();
  232. this->CMakeThread->wait(2000);
  233. }
  234. void CMakeSetupDialog::doConfigure()
  235. {
  236. if(this->CurrentState == Configuring)
  237. {
  238. // stop configure
  239. doInterrupt();
  240. return;
  241. }
  242. // make sure build directory exists
  243. QString bindir = this->CMakeThread->cmakeInstance()->binaryDirectory();
  244. QDir dir(bindir);
  245. if(!dir.exists())
  246. {
  247. QString msg = tr("Build directory does not exist, "
  248. "should I create it?")
  249. + "\n\n"
  250. + tr("Directory: ");
  251. msg += bindir;
  252. QString title = tr("Create Directory");
  253. QMessageBox::StandardButton btn;
  254. btn = QMessageBox::information(this, title, msg,
  255. QMessageBox::Yes | QMessageBox::No);
  256. if(btn == QMessageBox::No)
  257. {
  258. return;
  259. }
  260. if(!dir.mkpath("."))
  261. {
  262. QMessageBox::information(this, tr("Create Directory Failed"),
  263. QString(tr("Failed to create directory %1")).arg(dir.path()),
  264. QMessageBox::Ok);
  265. return;
  266. }
  267. }
  268. // if no generator, prompt for it and other setup stuff
  269. if(this->CMakeThread->cmakeInstance()->generator().isEmpty())
  270. {
  271. if(!this->setupFirstConfigure())
  272. {
  273. return;
  274. }
  275. }
  276. // remember path
  277. this->addBinaryPath(dir.absolutePath());
  278. this->enterState(Configuring);
  279. this->CacheValues->selectionModel()->clear();
  280. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  281. "setProperties", Qt::QueuedConnection,
  282. Q_ARG(QCMakePropertyList,
  283. this->CacheValues->cacheModel()->properties()));
  284. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  285. "configure", Qt::QueuedConnection);
  286. }
  287. void CMakeSetupDialog::finishConfigure(int err)
  288. {
  289. if(0 == err && !this->CacheValues->cacheModel()->newPropertyCount())
  290. {
  291. this->enterState(ReadyGenerate);
  292. }
  293. else
  294. {
  295. this->enterState(ReadyConfigure);
  296. this->CacheValues->scrollToTop();
  297. }
  298. if(err != 0)
  299. {
  300. QMessageBox::critical(this, tr("Error"),
  301. tr("Error in configuration process, project files may be invalid"),
  302. QMessageBox::Ok);
  303. }
  304. }
  305. void CMakeSetupDialog::finishGenerate(int err)
  306. {
  307. this->enterState(ReadyConfigure);
  308. if(err != 0)
  309. {
  310. QMessageBox::critical(this, tr("Error"),
  311. tr("Error in generation process, project files may be invalid"),
  312. QMessageBox::Ok);
  313. }
  314. }
  315. void CMakeSetupDialog::doInstallForCommandLine()
  316. {
  317. QMacInstallDialog setupdialog(0);
  318. setupdialog.exec();
  319. }
  320. void CMakeSetupDialog::doGenerate()
  321. {
  322. if(this->CurrentState == Generating)
  323. {
  324. // stop generate
  325. doInterrupt();
  326. return;
  327. }
  328. this->enterState(Generating);
  329. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  330. "generate", Qt::QueuedConnection);
  331. }
  332. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  333. {
  334. // prompt for close if there are unsaved changes, and we're not busy
  335. if(this->CacheModified)
  336. {
  337. QString msg = tr("You have changed options but not rebuilt, "
  338. "are you sure you want to exit?");
  339. QString title = tr("Confirm Exit");
  340. QMessageBox::StandardButton btn;
  341. btn = QMessageBox::critical(this, title, msg,
  342. QMessageBox::Yes | QMessageBox::No);
  343. if(btn == QMessageBox::No)
  344. {
  345. e->ignore();
  346. }
  347. }
  348. // don't close if we're busy, unless the user really wants to
  349. if(this->CurrentState == Configuring)
  350. {
  351. QString msg = "You are in the middle of a Configure.\n"
  352. "If you Exit now the configure information will be lost.\n"
  353. "Are you sure you want to Exit?";
  354. QString title = tr("Confirm Exit");
  355. QMessageBox::StandardButton btn;
  356. btn = QMessageBox::critical(this, title, msg,
  357. QMessageBox::Yes | QMessageBox::No);
  358. if(btn == QMessageBox::No)
  359. {
  360. e->ignore();
  361. }
  362. else
  363. {
  364. this->doInterrupt();
  365. }
  366. }
  367. // let the generate finish
  368. if(this->CurrentState == Generating)
  369. {
  370. e->ignore();
  371. }
  372. }
  373. void CMakeSetupDialog::doHelp()
  374. {
  375. QString msg = tr("CMake is used to configure and generate build files for "
  376. "software projects. The basic steps for configuring a project are as "
  377. "follows:\r\n\r\n1. Select the source directory for the project. This should "
  378. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the build "
  379. "directory for the project. This is the directory where the project will be "
  380. "built. It can be the same or a different directory than the source "
  381. "directory. For easy clean up, a separate build directory is recommended. "
  382. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the "
  383. "source and binary directories are selected, it is time to press the "
  384. "Configure button. This will cause CMake to read all of the input files and "
  385. "discover all the variables used by the project. The first time a variable "
  386. "is displayed it will be in Red. Users should inspect red variables making "
  387. "sure the values are correct. For some projects the Configure process can "
  388. "be iterative, so continue to press the Configure button until there are no "
  389. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you "
  390. "should click the Generate button. This will write the build files to the build "
  391. "directory.");
  392. QDialog dialog;
  393. QFontMetrics met(this->font());
  394. int msgWidth = met.width(msg);
  395. dialog.setMinimumSize(msgWidth/15,20);
  396. dialog.setWindowTitle(tr("Help"));
  397. QVBoxLayout* l = new QVBoxLayout(&dialog);
  398. QLabel* lab = new QLabel(&dialog);
  399. lab->setText(msg);
  400. lab->setWordWrap(true);
  401. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  402. Qt::Horizontal, &dialog);
  403. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  404. l->addWidget(lab);
  405. l->addWidget(btns);
  406. dialog.exec();
  407. }
  408. void CMakeSetupDialog::doInterrupt()
  409. {
  410. this->enterState(Interrupting);
  411. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  412. "interrupt", Qt::QueuedConnection);
  413. }
  414. void CMakeSetupDialog::doSourceBrowse()
  415. {
  416. QString dir = QFileDialog::getExistingDirectory(this,
  417. tr("Enter Path to Source"), this->SourceDirectory->text());
  418. if(!dir.isEmpty())
  419. {
  420. this->setSourceDirectory(dir);
  421. }
  422. }
  423. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  424. {
  425. if(this->SourceDirectory->text() != dir)
  426. {
  427. this->SourceDirectory->blockSignals(true);
  428. this->SourceDirectory->setText(dir);
  429. this->SourceDirectory->blockSignals(false);
  430. }
  431. }
  432. void CMakeSetupDialog::updateBinaryDirectory(const QString& dir)
  433. {
  434. if(this->BinaryDirectory->currentText() != dir)
  435. {
  436. this->BinaryDirectory->blockSignals(true);
  437. this->BinaryDirectory->setEditText(dir);
  438. this->BinaryDirectory->blockSignals(false);
  439. }
  440. }
  441. void CMakeSetupDialog::doBinaryBrowse()
  442. {
  443. QString dir = QFileDialog::getExistingDirectory(this,
  444. tr("Enter Path to Build"), this->BinaryDirectory->currentText());
  445. if(!dir.isEmpty() && dir != this->BinaryDirectory->currentText())
  446. {
  447. this->setBinaryDirectory(dir);
  448. }
  449. }
  450. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  451. {
  452. this->BinaryDirectory->setEditText(dir);
  453. }
  454. void CMakeSetupDialog::onSourceDirectoryChanged(const QString& dir)
  455. {
  456. this->Output->clear();
  457. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  458. "setSourceDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  459. }
  460. void CMakeSetupDialog::onBinaryDirectoryChanged(const QString& dir)
  461. {
  462. QString title = QString(tr("CMake %1 - %2"));
  463. title = title.arg(cmVersion::GetCMakeVersion());
  464. title = title.arg(dir);
  465. this->setWindowTitle(title);
  466. this->CacheModified = false;
  467. this->CacheValues->cacheModel()->clear();
  468. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())->clearChanges();
  469. this->Output->clear();
  470. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  471. "setBinaryDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  472. }
  473. void CMakeSetupDialog::setSourceDirectory(const QString& dir)
  474. {
  475. this->SourceDirectory->setText(dir);
  476. }
  477. void CMakeSetupDialog::showProgress(const QString& /*msg*/, float percent)
  478. {
  479. this->ProgressBar->setValue(qRound(percent * 100));
  480. }
  481. void CMakeSetupDialog::error(const QString& msg)
  482. {
  483. this->Output->setCurrentCharFormat(this->ErrorFormat);
  484. this->Output->append(msg);
  485. }
  486. void CMakeSetupDialog::message(const QString& msg)
  487. {
  488. this->Output->setCurrentCharFormat(this->MessageFormat);
  489. this->Output->append(msg);
  490. }
  491. void CMakeSetupDialog::setEnabledState(bool enabled)
  492. {
  493. // disable parts of the GUI during configure/generate
  494. this->CacheValues->cacheModel()->setEditEnabled(enabled);
  495. this->SourceDirectory->setEnabled(enabled);
  496. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  497. this->BinaryDirectory->setEnabled(enabled);
  498. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  499. this->ReloadCacheAction->setEnabled(enabled);
  500. this->DeleteCacheAction->setEnabled(enabled);
  501. this->ExitAction->setEnabled(enabled);
  502. this->ConfigureAction->setEnabled(enabled);
  503. this->AddEntry->setEnabled(enabled);
  504. this->RemoveEntry->setEnabled(false); // let selection re-enable it
  505. }
  506. bool CMakeSetupDialog::setupFirstConfigure()
  507. {
  508. FirstConfigure dialog;
  509. // initialize dialog and restore saved settings
  510. // add generators
  511. dialog.setGenerators(this->CMakeThread->cmakeInstance()->availableGenerators());
  512. // restore from settings
  513. dialog.loadFromSettings();
  514. if(dialog.exec() == QDialog::Accepted)
  515. {
  516. dialog.saveToSettings();
  517. this->CMakeThread->cmakeInstance()->setGenerator(dialog.getGenerator());
  518. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  519. if(dialog.compilerSetup())
  520. {
  521. QString fortranCompiler = dialog.getFortranCompiler();
  522. if(!fortranCompiler.isEmpty())
  523. {
  524. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  525. "Fortran compiler.", fortranCompiler, false);
  526. }
  527. QString cxxCompiler = dialog.getCXXCompiler();
  528. if(!cxxCompiler.isEmpty())
  529. {
  530. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  531. "CXX compiler.", cxxCompiler, false);
  532. }
  533. QString cCompiler = dialog.getCCompiler();
  534. if(!cCompiler.isEmpty())
  535. {
  536. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  537. "C compiler.", cCompiler, false);
  538. }
  539. }
  540. else if(dialog.crossCompilerSetup())
  541. {
  542. QString fortranCompiler = dialog.getFortranCompiler();
  543. if(!fortranCompiler.isEmpty())
  544. {
  545. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  546. "Fortran compiler.", fortranCompiler, false);
  547. }
  548. QString mode = dialog.getCrossIncludeMode();
  549. m->insertProperty(QCMakeProperty::STRING, "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE",
  550. "CMake Find Include Mode", mode, false);
  551. mode = dialog.getCrossLibraryMode();
  552. m->insertProperty(QCMakeProperty::STRING, "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY",
  553. "CMake Find Library Mode", mode, false);
  554. mode = dialog.getCrossProgramMode();
  555. m->insertProperty(QCMakeProperty::STRING, "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM",
  556. "CMake Find Program Mode", mode, false);
  557. QString rootPath = dialog.getCrossRoot();
  558. m->insertProperty(QCMakeProperty::PATH, "CMAKE_FIND_ROOT_PATH",
  559. "CMake Find Root Path", rootPath, false);
  560. QString systemName = dialog.getSystemName();
  561. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_NAME",
  562. "CMake System Name", systemName, false);
  563. QString cxxCompiler = dialog.getCXXCompiler();
  564. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  565. "CXX compiler.", cxxCompiler, false);
  566. QString cCompiler = dialog.getCCompiler();
  567. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  568. "C compiler.", cCompiler, false);
  569. }
  570. else if(dialog.crossCompilerToolChainFile())
  571. {
  572. QString toolchainFile = dialog.getCrossCompilerToolChainFile();
  573. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_TOOLCHAIN_FILE",
  574. "Cross Compile ToolChain File", toolchainFile, false);
  575. }
  576. return true;
  577. }
  578. return false;
  579. }
  580. void CMakeSetupDialog::updateGeneratorLabel(const QString& gen)
  581. {
  582. QString str = tr("Current Generator: ");
  583. if(gen.isEmpty())
  584. {
  585. str += tr("None");
  586. }
  587. else
  588. {
  589. str += gen;
  590. }
  591. this->Generator->setText(str);
  592. }
  593. void CMakeSetupDialog::doReloadCache()
  594. {
  595. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  596. "reloadCache", Qt::QueuedConnection);
  597. }
  598. void CMakeSetupDialog::doDeleteCache()
  599. {
  600. QString title = tr("Delete Cache");
  601. QString msg = "Are you sure you want to delete the cache?";
  602. QMessageBox::StandardButton btn;
  603. btn = QMessageBox::information(this, title, msg,
  604. QMessageBox::Yes | QMessageBox::No);
  605. if(btn == QMessageBox::No)
  606. {
  607. return;
  608. }
  609. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  610. "deleteCache", Qt::QueuedConnection);
  611. }
  612. void CMakeSetupDialog::doAbout()
  613. {
  614. QString msg = "CMake %1\n"
  615. "Using Qt %2\n"
  616. "www.cmake.org";
  617. msg = msg.arg(cmVersion::GetCMakeVersion());
  618. msg = msg.arg(qVersion());
  619. QDialog dialog;
  620. dialog.setWindowTitle(tr("About"));
  621. QVBoxLayout* l = new QVBoxLayout(&dialog);
  622. QLabel* lab = new QLabel(&dialog);
  623. l->addWidget(lab);
  624. lab->setText(msg);
  625. lab->setWordWrap(true);
  626. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  627. Qt::Horizontal, &dialog);
  628. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  629. l->addWidget(btns);
  630. dialog.exec();
  631. }
  632. void CMakeSetupDialog::setExitAfterGenerate(bool b)
  633. {
  634. this->ExitAfterGenerate = b;
  635. }
  636. void CMakeSetupDialog::addBinaryPath(const QString& path)
  637. {
  638. QString cleanpath = QDir::cleanPath(path);
  639. // update UI
  640. this->BinaryDirectory->blockSignals(true);
  641. int idx = this->BinaryDirectory->findText(cleanpath);
  642. if(idx != -1)
  643. {
  644. this->BinaryDirectory->removeItem(idx);
  645. }
  646. this->BinaryDirectory->insertItem(0, cleanpath);
  647. this->BinaryDirectory->setCurrentIndex(0);
  648. this->BinaryDirectory->blockSignals(false);
  649. // save to registry
  650. QStringList buildPaths = this->loadBuildPaths();
  651. buildPaths.removeAll(cleanpath);
  652. buildPaths.prepend(cleanpath);
  653. this->saveBuildPaths(buildPaths);
  654. }
  655. void CMakeSetupDialog::dragEnterEvent(QDragEnterEvent* e)
  656. {
  657. if(!(this->CurrentState == ReadyConfigure ||
  658. this->CurrentState == ReadyGenerate))
  659. {
  660. e->ignore();
  661. return;
  662. }
  663. const QMimeData* dat = e->mimeData();
  664. QList<QUrl> urls = dat->urls();
  665. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  666. if(!file.isEmpty() &&
  667. (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive) ||
  668. file.endsWith("CMakeLists.txt", Qt::CaseInsensitive) ) )
  669. {
  670. e->accept();
  671. }
  672. else
  673. {
  674. e->ignore();
  675. }
  676. }
  677. void CMakeSetupDialog::dropEvent(QDropEvent* e)
  678. {
  679. if(!(this->CurrentState == ReadyConfigure ||
  680. this->CurrentState == ReadyGenerate))
  681. {
  682. return;
  683. }
  684. const QMimeData* dat = e->mimeData();
  685. QList<QUrl> urls = dat->urls();
  686. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  687. if(file.endsWith("CMakeCache.txt", Qt::CaseInsensitive))
  688. {
  689. QFileInfo info(file);
  690. if(this->CMakeThread->cmakeInstance()->binaryDirectory() != info.absolutePath())
  691. {
  692. this->setBinaryDirectory(info.absolutePath());
  693. }
  694. }
  695. else if(file.endsWith("CMakeLists.txt", Qt::CaseInsensitive))
  696. {
  697. QFileInfo info(file);
  698. if(this->CMakeThread->cmakeInstance()->binaryDirectory() != info.absolutePath())
  699. {
  700. this->setSourceDirectory(info.absolutePath());
  701. this->setBinaryDirectory(info.absolutePath());
  702. }
  703. }
  704. }
  705. QStringList CMakeSetupDialog::loadBuildPaths()
  706. {
  707. QSettings settings;
  708. settings.beginGroup("Settings/StartPath");
  709. QStringList buildPaths;
  710. for(int i=0; i<10; i++)
  711. {
  712. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  713. if(!p.isEmpty())
  714. {
  715. buildPaths.append(p);
  716. }
  717. }
  718. return buildPaths;
  719. }
  720. void CMakeSetupDialog::saveBuildPaths(const QStringList& paths)
  721. {
  722. QSettings settings;
  723. settings.beginGroup("Settings/StartPath");
  724. int num = paths.count();
  725. if(num > 10)
  726. {
  727. num = 10;
  728. }
  729. for(int i=0; i<num; i++)
  730. {
  731. settings.setValue(QString("WhereBuild%1").arg(i), paths[i]);
  732. }
  733. }
  734. void CMakeSetupDialog::setCacheModified()
  735. {
  736. this->CacheModified = true;
  737. this->enterState(ReadyConfigure);
  738. }
  739. void CMakeSetupDialog::removeSelectedCacheEntries()
  740. {
  741. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  742. QList<QPersistentModelIndex> pidxs;
  743. foreach(QModelIndex i, idxs)
  744. {
  745. pidxs.append(i);
  746. }
  747. foreach(QPersistentModelIndex pi, pidxs)
  748. {
  749. this->CacheValues->model()->removeRow(pi.row(), pi.parent());
  750. }
  751. }
  752. void CMakeSetupDialog::selectionChanged()
  753. {
  754. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  755. if(idxs.count() &&
  756. (this->CurrentState == ReadyConfigure ||
  757. this->CurrentState == ReadyGenerate) )
  758. {
  759. this->RemoveEntry->setEnabled(true);
  760. }
  761. else
  762. {
  763. this->RemoveEntry->setEnabled(false);
  764. }
  765. }
  766. void CMakeSetupDialog::enterState(CMakeSetupDialog::State s)
  767. {
  768. if(s == this->CurrentState)
  769. {
  770. return;
  771. }
  772. this->CurrentState = s;
  773. if(s == Interrupting)
  774. {
  775. this->ConfigureButton->setEnabled(false);
  776. this->GenerateButton->setEnabled(false);
  777. }
  778. else if(s == Configuring)
  779. {
  780. this->Output->clear();
  781. this->setEnabledState(false);
  782. this->GenerateButton->setEnabled(false);
  783. this->GenerateAction->setEnabled(false);
  784. this->ConfigureButton->setText(tr("&Stop"));
  785. }
  786. else if(s == Generating)
  787. {
  788. this->CacheModified = false;
  789. this->setEnabledState(false);
  790. this->ConfigureButton->setEnabled(false);
  791. this->GenerateAction->setEnabled(false);
  792. this->GenerateButton->setText(tr("&Stop"));
  793. }
  794. else if(s == ReadyConfigure)
  795. {
  796. this->ProgressBar->reset();
  797. this->setEnabledState(true);
  798. this->GenerateButton->setEnabled(false);
  799. this->GenerateAction->setEnabled(false);
  800. this->ConfigureButton->setEnabled(true);
  801. this->ConfigureButton->setText(tr("&Configure"));
  802. this->GenerateButton->setText(tr("&Generate"));
  803. }
  804. else if(s == ReadyGenerate)
  805. {
  806. this->ProgressBar->reset();
  807. this->setEnabledState(true);
  808. this->GenerateButton->setEnabled(true);
  809. this->GenerateAction->setEnabled(true);
  810. this->ConfigureButton->setEnabled(true);
  811. this->ConfigureButton->setText(tr("&Configure"));
  812. this->GenerateButton->setText(tr("&Generate"));
  813. }
  814. }
  815. void CMakeSetupDialog::addCacheEntry()
  816. {
  817. QDialog dialog(this);
  818. dialog.resize(400, 200);
  819. dialog.setWindowTitle(tr("Add Cache Entry"));
  820. QVBoxLayout* l = new QVBoxLayout(&dialog);
  821. AddCacheEntry* w = new AddCacheEntry(&dialog);
  822. QDialogButtonBox* btns = new QDialogButtonBox(
  823. QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
  824. Qt::Horizontal, &dialog);
  825. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  826. QObject::connect(btns, SIGNAL(rejected()), &dialog, SLOT(reject()));
  827. l->addWidget(w);
  828. l->addStretch();
  829. l->addWidget(btns);
  830. if(QDialog::Accepted == dialog.exec())
  831. {
  832. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  833. m->insertProperty(w->type(), w->name(), w->description(), w->value(), false);
  834. }
  835. }
  836. void CMakeSetupDialog::startSearch()
  837. {
  838. this->Search->setFocus(Qt::OtherFocusReason);
  839. this->Search->selectAll();
  840. }
  841. void CMakeSetupDialog::setDebugOutput(bool flag)
  842. {
  843. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  844. "setDebugOutput", Qt::QueuedConnection, Q_ARG(bool, flag));
  845. }
  846. void CMakeSetupDialog::setGroupedView(bool v)
  847. {
  848. this->CacheValues->cacheModel()->setViewType(v ? QCMakeCacheModel::GroupView : QCMakeCacheModel::FlatView);
  849. this->CacheValues->setRootIsDecorated(v);
  850. QSettings settings;
  851. settings.beginGroup("Settings/StartPath");
  852. settings.setValue("GroupView", v);
  853. }
  854. void CMakeSetupDialog::setAdvancedView(bool v)
  855. {
  856. this->CacheValues->setShowAdvanced(v);
  857. }
  858. void CMakeSetupDialog::showUserChanges()
  859. {
  860. QSet<QCMakeProperty> changes =
  861. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())->changes();
  862. QDialog dialog(this);
  863. dialog.setWindowTitle(tr("My Changes"));
  864. dialog.resize(600, 400);
  865. QVBoxLayout* l = new QVBoxLayout(&dialog);
  866. QTextEdit* textedit = new QTextEdit(&dialog);
  867. textedit->setReadOnly(true);
  868. l->addWidget(textedit);
  869. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Close,
  870. Qt::Horizontal, &dialog);
  871. QObject::connect(btns, SIGNAL(rejected()), &dialog, SLOT(accept()));
  872. l->addWidget(btns);
  873. QString command;
  874. QString cache;
  875. foreach(QCMakeProperty prop, changes)
  876. {
  877. QString type;
  878. switch(prop.Type)
  879. {
  880. case QCMakeProperty::BOOL:
  881. type = "BOOL";
  882. break;
  883. case QCMakeProperty::PATH:
  884. type = "PATH";
  885. break;
  886. case QCMakeProperty::FILEPATH:
  887. type = "FILEPATH";
  888. break;
  889. case QCMakeProperty::STRING:
  890. type = "STRING";
  891. break;
  892. }
  893. QString value;
  894. if(prop.Type == QCMakeProperty::BOOL)
  895. {
  896. value = prop.Value.toBool() ? "1" : "0";
  897. }
  898. else
  899. {
  900. value = prop.Value.toString();
  901. }
  902. QString line("%1:%2=");
  903. line = line.arg(prop.Key);
  904. line = line.arg(type);
  905. command += QString("-D%1\"%2\" ").arg(line).arg(value);
  906. cache += QString("%1%2\n").arg(line).arg(value);
  907. }
  908. textedit->append(tr("Commandline options:"));
  909. textedit->append(command);
  910. textedit->append("\n");
  911. textedit->append(tr("Cache file:"));
  912. textedit->append(cache);
  913. dialog.exec();
  914. }
  915. void CMakeSetupDialog::setSearchFilter(const QString& str)
  916. {
  917. this->CacheValues->selectionModel()->clear();
  918. this->CacheValues->setSearchFilter(str);
  919. }