CMakeSetupDialog.cxx 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "CMakeSetupDialog.h"
  4. #include <cm/memory>
  5. #include <QCloseEvent>
  6. #include <QCoreApplication>
  7. #include <QDesktopServices>
  8. #include <QDialogButtonBox>
  9. #include <QDragEnterEvent>
  10. #include <QFileDialog>
  11. #include <QInputDialog>
  12. #include <QKeySequence>
  13. #include <QMenu>
  14. #include <QMenuBar>
  15. #include <QMessageBox>
  16. #include <QMimeData>
  17. #include <QProcessEnvironment>
  18. #include <QProgressBar>
  19. #include <QSettings>
  20. #include <QShortcut>
  21. #include <QStatusBar>
  22. #include <QToolButton>
  23. #include <QUrl>
  24. #ifdef QT_WINEXTRAS
  25. # include <QWinTaskbarButton>
  26. # include <QWinTaskbarProgress>
  27. #endif
  28. #include "QCMake.h"
  29. #include "QCMakeCacheView.h"
  30. #include "cmSystemTools.h"
  31. #include "cmVersion.h"
  32. #include "AddCacheEntry.h"
  33. #include "EnvironmentDialog.h"
  34. #include "FirstConfigure.h"
  35. #include "RegexExplorer.h"
  36. #include "WarningMessagesDialog.h"
  37. QCMakeThread::QCMakeThread(QObject* p)
  38. : QThread(p)
  39. {
  40. }
  41. QCMake* QCMakeThread::cmakeInstance() const
  42. {
  43. return this->CMakeInstance.get();
  44. }
  45. void QCMakeThread::run()
  46. {
  47. this->CMakeInstance = cm::make_unique<QCMake>();
  48. // emit that this cmake thread is ready for use
  49. emit this->cmakeInitialized();
  50. this->exec();
  51. this->CMakeInstance.reset();
  52. }
  53. CMakeSetupDialog::CMakeSetupDialog()
  54. : ExitAfterGenerate(true)
  55. , CacheModified(false)
  56. , ConfigureNeeded(true)
  57. , CurrentState(Interrupting)
  58. {
  59. QString title = QString(tr("CMake %1"));
  60. title = title.arg(cmVersion::GetCMakeVersion());
  61. this->setWindowTitle(title);
  62. // create the GUI
  63. QSettings settings;
  64. settings.beginGroup("Settings/StartPath");
  65. restoreGeometry(settings.value("geometry").toByteArray());
  66. restoreState(settings.value("windowState").toByteArray());
  67. this->AddVariableNames =
  68. settings.value("AddVariableNames", QStringList("CMAKE_INSTALL_PREFIX"))
  69. .toStringList();
  70. this->AddVariableTypes =
  71. settings.value("AddVariableTypes", QStringList("PATH")).toStringList();
  72. QWidget* cont = new QWidget(this);
  73. this->setupUi(cont);
  74. this->Splitter->setStretchFactor(0, 3);
  75. this->Splitter->setStretchFactor(1, 1);
  76. this->setCentralWidget(cont);
  77. this->ProgressBar->reset();
  78. this->RemoveEntry->setEnabled(false);
  79. this->AddEntry->setEnabled(false);
  80. QByteArray p = settings.value("SplitterSizes").toByteArray();
  81. this->Splitter->restoreState(p);
  82. bool groupView = settings.value("GroupView", false).toBool();
  83. this->setGroupedView(groupView);
  84. this->groupedCheck->setCheckState(groupView ? Qt::Checked : Qt::Unchecked);
  85. bool advancedView = settings.value("AdvancedView", false).toBool();
  86. this->setAdvancedView(advancedView);
  87. this->advancedCheck->setCheckState(advancedView ? Qt::Checked
  88. : Qt::Unchecked);
  89. QMenu* FileMenu = this->menuBar()->addMenu(tr("&File"));
  90. this->ReloadCacheAction = FileMenu->addAction(tr("&Reload Cache"));
  91. QObject::connect(this->ReloadCacheAction, &QAction::triggered, this,
  92. &CMakeSetupDialog::doReloadCache);
  93. this->DeleteCacheAction = FileMenu->addAction(tr("&Delete Cache"));
  94. QObject::connect(this->DeleteCacheAction, &QAction::triggered, this,
  95. &CMakeSetupDialog::doDeleteCache);
  96. this->ExitAction = FileMenu->addAction(tr("E&xit"));
  97. QObject::connect(this->ExitAction, &QAction::triggered, this,
  98. &CMakeSetupDialog::close);
  99. this->ExitAction->setShortcut(QKeySequence::Quit);
  100. QMenu* ToolsMenu = this->menuBar()->addMenu(tr("&Tools"));
  101. this->ConfigureAction = ToolsMenu->addAction(tr("&Configure"));
  102. QObject::connect(this->ConfigureAction, &QAction::triggered, this,
  103. &CMakeSetupDialog::doConfigure);
  104. // prevent merging with Preferences menu item on macOS
  105. this->ConfigureAction->setMenuRole(QAction::NoRole);
  106. this->GenerateAction = ToolsMenu->addAction(tr("&Generate"));
  107. QObject::connect(this->GenerateAction, &QAction::triggered, this,
  108. &CMakeSetupDialog::doGenerate);
  109. auto* a = ToolsMenu->addAction(tr("&Show My Changes"));
  110. QObject::connect(a, &QAction::triggered, this,
  111. &CMakeSetupDialog::showUserChanges);
  112. #if defined(Q_WS_MAC) || defined(Q_OS_MAC)
  113. this->InstallForCommandLineAction =
  114. ToolsMenu->addAction(tr("&How to Install For Command Line Use"));
  115. QObject::connect(this->InstallForCommandLineAction, &QAction::triggered,
  116. this, &CMakeSetupDialog::doInstallForCommandLine);
  117. #endif
  118. ToolsMenu->addSeparator();
  119. a = ToolsMenu->addAction(tr("Regular Expression Explorer..."));
  120. QObject::connect(a, &QAction::triggered, this,
  121. &CMakeSetupDialog::doRegexExplorerDialog);
  122. ToolsMenu->addSeparator();
  123. a = ToolsMenu->addAction(tr("&Find in Output..."));
  124. QObject::connect(a, &QAction::triggered, this,
  125. &CMakeSetupDialog::doOutputFindDialog);
  126. a->setShortcut(QKeySequence::Find);
  127. a = ToolsMenu->addAction(tr("Find Next"));
  128. QObject::connect(a, &QAction::triggered, this,
  129. &CMakeSetupDialog::doOutputFindNext);
  130. a->setShortcut(QKeySequence::FindNext);
  131. a = ToolsMenu->addAction(tr("Find Previous"));
  132. QObject::connect(a, &QAction::triggered, this,
  133. &CMakeSetupDialog::doOutputFindPrev);
  134. a->setShortcut(QKeySequence::FindPrevious);
  135. a = ToolsMenu->addAction(tr("Goto Next Error")); // in Visual Studio
  136. QObject::connect(a, &QAction::triggered, this,
  137. &CMakeSetupDialog::doOutputErrorNext);
  138. a->setShortcut(QKeySequence(Qt::Key_F8));
  139. auto* s = new QShortcut(this);
  140. s->setKey(QKeySequence(Qt::CTRL + Qt::Key_Period));
  141. QObject::connect(s, &QShortcut::activated, this,
  142. &CMakeSetupDialog::doOutputErrorNext); // in Eclipse
  143. QMenu* OptionsMenu = this->menuBar()->addMenu(tr("&Options"));
  144. a = OptionsMenu->addAction(tr("Warning Messages..."));
  145. QObject::connect(a, &QAction::triggered, this,
  146. &CMakeSetupDialog::doWarningMessagesDialog);
  147. this->WarnUninitializedAction =
  148. OptionsMenu->addAction(tr("&Warn Uninitialized (--warn-uninitialized)"));
  149. this->WarnUninitializedAction->setCheckable(true);
  150. QAction* debugAction = OptionsMenu->addAction(tr("&Debug Output"));
  151. debugAction->setCheckable(true);
  152. QObject::connect(debugAction, &QAction::toggled, this,
  153. &CMakeSetupDialog::setDebugOutput);
  154. OptionsMenu->addSeparator();
  155. a = OptionsMenu->addAction(tr("&Expand Grouped Entries"));
  156. QObject::connect(a, &QAction::triggered, this->CacheValues,
  157. &QCMakeCacheView::expandAll);
  158. a = OptionsMenu->addAction(tr("&Collapse Grouped Entries"));
  159. QObject::connect(a, &QAction::triggered, this->CacheValues,
  160. &QCMakeCacheView::collapseAll);
  161. QMenu* HelpMenu = this->menuBar()->addMenu(tr("&Help"));
  162. a = HelpMenu->addAction(tr("Help"));
  163. QObject::connect(a, &QAction::triggered, this, &CMakeSetupDialog::doHelp);
  164. a->setShortcut(QKeySequence::HelpContents);
  165. a = HelpMenu->addAction(tr("CMake Reference Manual"));
  166. QObject::connect(a, &QAction::triggered, this, []() {
  167. QString urlFormat("https://cmake.org/cmake/help/v%1.%2/");
  168. QUrl url(urlFormat.arg(QString::number(cmVersion::GetMajorVersion()),
  169. QString::number(cmVersion::GetMinorVersion())));
  170. if (!cmSystemTools::GetHTMLDoc().empty()) {
  171. url = QUrl::fromLocalFile(
  172. QDir(QString::fromLocal8Bit(cmSystemTools::GetHTMLDoc().data()))
  173. .filePath("index.html"));
  174. }
  175. QDesktopServices::openUrl(url);
  176. });
  177. a = HelpMenu->addAction(tr("About"));
  178. QObject::connect(a, &QAction::triggered, this, &CMakeSetupDialog::doAbout);
  179. this->setAcceptDrops(true);
  180. // get the saved binary directories
  181. QStringList buildPaths = this->loadBuildPaths();
  182. this->BinaryDirectory->addItems(buildPaths);
  183. this->BinaryDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  184. this->SourceDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  185. // fixed pitch font in output window
  186. QFont outputFont("Courier");
  187. this->Output->setFont(outputFont);
  188. this->ErrorFormat.setForeground(QBrush(Qt::red));
  189. this->Output->setContextMenuPolicy(Qt::CustomContextMenu);
  190. connect(this->Output, &QTextEdit::customContextMenuRequested, this,
  191. &CMakeSetupDialog::doOutputContextMenu);
  192. // disable open project button
  193. this->OpenProjectButton->setDisabled(true);
  194. // start the cmake worker thread
  195. this->CMakeThread = new QCMakeThread(this);
  196. QObject::connect(this->CMakeThread, &QCMakeThread::cmakeInitialized, this,
  197. &CMakeSetupDialog::initialize, Qt::QueuedConnection);
  198. this->CMakeThread->start();
  199. this->enterState(ReadyConfigure);
  200. ProgressOffset = 0.0;
  201. ProgressFactor = 1.0;
  202. }
  203. void CMakeSetupDialog::initialize()
  204. {
  205. // now the cmake worker thread is running, lets make our connections to it
  206. QObject::connect(this->CMakeThread->cmakeInstance(),
  207. &QCMake::propertiesChanged, this->CacheValues->cacheModel(),
  208. &QCMakeCacheModel::setProperties);
  209. QObject::connect(this->ConfigureButton, &QPushButton::clicked, this,
  210. &CMakeSetupDialog::doConfigure);
  211. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::configureDone,
  212. this, &CMakeSetupDialog::exitLoop);
  213. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::generateDone,
  214. this, &CMakeSetupDialog::exitLoop);
  215. QObject::connect(this->GenerateButton, &QPushButton::clicked, this,
  216. &CMakeSetupDialog::doGenerate);
  217. QObject::connect(this->OpenProjectButton, &QPushButton::clicked, this,
  218. &CMakeSetupDialog::doOpenProject);
  219. QObject::connect(this->BrowseSourceDirectoryButton, &QPushButton::clicked,
  220. this, &CMakeSetupDialog::doSourceBrowse);
  221. QObject::connect(this->BrowseBinaryDirectoryButton, &QPushButton::clicked,
  222. this, &CMakeSetupDialog::doBinaryBrowse);
  223. QObject::connect(this->BinaryDirectory, &QComboBox::editTextChanged, this,
  224. &CMakeSetupDialog::onBinaryDirectoryChanged);
  225. QObject::connect(this->SourceDirectory, &QLineEdit::textChanged, this,
  226. &CMakeSetupDialog::onSourceDirectoryChanged);
  227. QObject::connect(this->CMakeThread->cmakeInstance(),
  228. &QCMake::sourceDirChanged, this,
  229. &CMakeSetupDialog::updateSourceDirectory);
  230. QObject::connect(this->CMakeThread->cmakeInstance(),
  231. &QCMake::binaryDirChanged, this,
  232. &CMakeSetupDialog::updateBinaryDirectory);
  233. QObject::connect(this->CMakeThread->cmakeInstance(),
  234. &QCMake::progressChanged, this,
  235. &CMakeSetupDialog::showProgress);
  236. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::errorMessage,
  237. this, &CMakeSetupDialog::error);
  238. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::outputMessage,
  239. this, &CMakeSetupDialog::message);
  240. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::openPossible,
  241. this->OpenProjectButton, &CMakeSetupDialog::setEnabled);
  242. QObject::connect(this->groupedCheck, &QCheckBox::toggled, this,
  243. &CMakeSetupDialog::setGroupedView);
  244. QObject::connect(this->advancedCheck, &QCheckBox::toggled, this,
  245. &CMakeSetupDialog::setAdvancedView);
  246. QObject::connect(this->Search, &QLineEdit::textChanged, this,
  247. &CMakeSetupDialog::setSearchFilter);
  248. QObject::connect(this->CMakeThread->cmakeInstance(),
  249. &QCMake::generatorChanged, this,
  250. &CMakeSetupDialog::updateGeneratorLabel);
  251. this->updateGeneratorLabel(QString());
  252. QObject::connect(this->CacheValues->cacheModel(),
  253. &QCMakeCacheModel::dataChanged, this,
  254. &CMakeSetupDialog::setCacheModified);
  255. QObject::connect(this->CacheValues->selectionModel(),
  256. &QItemSelectionModel::selectionChanged, this,
  257. &CMakeSetupDialog::selectionChanged);
  258. QObject::connect(this->RemoveEntry, &QToolButton::clicked, this,
  259. &CMakeSetupDialog::removeSelectedCacheEntries);
  260. QObject::connect(this->AddEntry, &QToolButton::clicked, this,
  261. &CMakeSetupDialog::addCacheEntry);
  262. QObject::connect(this->Environment, &QToolButton::clicked, this,
  263. &CMakeSetupDialog::editEnvironment);
  264. QObject::connect(this->WarnUninitializedAction, &QAction::triggered,
  265. this->CMakeThread->cmakeInstance(),
  266. &QCMake::setWarnUninitializedMode);
  267. if (!this->SourceDirectory->text().isEmpty() ||
  268. !this->BinaryDirectory->lineEdit()->text().isEmpty()) {
  269. this->onSourceDirectoryChanged(this->SourceDirectory->text());
  270. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  271. } else {
  272. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  273. }
  274. #ifdef QT_WINEXTRAS
  275. this->TaskbarButton = new QWinTaskbarButton(this);
  276. this->TaskbarButton->setWindow(this->windowHandle());
  277. #endif
  278. }
  279. CMakeSetupDialog::~CMakeSetupDialog()
  280. {
  281. QSettings settings;
  282. settings.beginGroup("Settings/StartPath");
  283. settings.setValue("windowState", QVariant(saveState()));
  284. settings.setValue("geometry", QVariant(saveGeometry()));
  285. settings.setValue("SplitterSizes", this->Splitter->saveState());
  286. // wait for thread to stop
  287. this->CMakeThread->quit();
  288. this->CMakeThread->wait();
  289. }
  290. bool CMakeSetupDialog::prepareConfigure()
  291. {
  292. // make sure build directory exists
  293. QString bindir = this->CMakeThread->cmakeInstance()->binaryDirectory();
  294. QDir dir(bindir);
  295. if (!dir.exists()) {
  296. QString msg = tr("Build directory does not exist, "
  297. "should I create it?\n\n"
  298. "Directory: ");
  299. msg += bindir;
  300. QString title = tr("Create Directory");
  301. QMessageBox::StandardButton btn;
  302. btn = QMessageBox::information(this, title, msg,
  303. QMessageBox::Yes | QMessageBox::No);
  304. if (btn == QMessageBox::No) {
  305. return false;
  306. }
  307. if (!dir.mkpath(".")) {
  308. QMessageBox::information(
  309. this, tr("Create Directory Failed"),
  310. QString(tr("Failed to create directory %1")).arg(dir.path()),
  311. QMessageBox::Ok);
  312. return false;
  313. }
  314. }
  315. // if no generator, prompt for it and other setup stuff
  316. if (this->CMakeThread->cmakeInstance()->generator().isEmpty()) {
  317. if (!this->setupFirstConfigure()) {
  318. return false;
  319. }
  320. }
  321. // remember path
  322. this->addBinaryPath(dir.absolutePath());
  323. return true;
  324. }
  325. void CMakeSetupDialog::exitLoop(int err)
  326. {
  327. this->LocalLoop.exit(err);
  328. }
  329. void CMakeSetupDialog::doConfigure()
  330. {
  331. if (this->CurrentState == Configuring) {
  332. // stop configure
  333. doInterrupt();
  334. return;
  335. }
  336. if (!prepareConfigure()) {
  337. return;
  338. }
  339. this->enterState(Configuring);
  340. bool ret = doConfigureInternal();
  341. if (ret) {
  342. this->ConfigureNeeded = false;
  343. }
  344. if (ret && !this->CacheValues->cacheModel()->newPropertyCount()) {
  345. this->enterState(ReadyGenerate);
  346. } else {
  347. this->enterState(ReadyConfigure);
  348. this->CacheValues->scrollToTop();
  349. }
  350. this->ProgressBar->reset();
  351. #ifdef QT_WINEXTRAS
  352. this->TaskbarButton->progress()->reset();
  353. #endif
  354. }
  355. bool CMakeSetupDialog::doConfigureInternal()
  356. {
  357. this->Output->clear();
  358. this->CacheValues->selectionModel()->clear();
  359. QMetaObject::invokeMethod(
  360. this->CMakeThread->cmakeInstance(), "setProperties", Qt::QueuedConnection,
  361. Q_ARG(QCMakePropertyList, this->CacheValues->cacheModel()->properties()));
  362. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "configure",
  363. Qt::QueuedConnection);
  364. int err = this->LocalLoop.exec();
  365. if (err != 0) {
  366. QMessageBox::critical(
  367. this, tr("Error"),
  368. tr("Error in configuration process, project files may be invalid"),
  369. QMessageBox::Ok);
  370. }
  371. return 0 == err;
  372. }
  373. void CMakeSetupDialog::doInstallForCommandLine()
  374. {
  375. QString title = tr("How to Install For Command Line Use");
  376. QString msg = tr("One may add CMake to the PATH:\n"
  377. "\n"
  378. " PATH=\"%1\":\"$PATH\"\n"
  379. "\n"
  380. "Or, to install symlinks to '/usr/local/bin', run:\n"
  381. "\n"
  382. " sudo \"%2\" --install\n"
  383. "\n"
  384. "Or, to install symlinks to another directory, run:\n"
  385. "\n"
  386. " sudo \"%3\" --install=/path/to/bin\n");
  387. msg = msg.arg(
  388. cmSystemTools::GetFilenamePath(cmSystemTools::GetCMakeCommand()).c_str());
  389. msg = msg.arg(cmSystemTools::GetCMakeGUICommand().c_str());
  390. msg = msg.arg(cmSystemTools::GetCMakeGUICommand().c_str());
  391. QDialog dialog;
  392. dialog.setWindowTitle(title);
  393. QVBoxLayout* l = new QVBoxLayout(&dialog);
  394. QLabel* lab = new QLabel(&dialog);
  395. l->addWidget(lab);
  396. lab->setText(msg);
  397. lab->setWordWrap(false);
  398. lab->setTextInteractionFlags(Qt::TextSelectableByMouse);
  399. QDialogButtonBox* btns =
  400. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  401. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  402. &QDialog::accept);
  403. l->addWidget(btns);
  404. dialog.exec();
  405. }
  406. bool CMakeSetupDialog::doGenerateInternal()
  407. {
  408. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "generate",
  409. Qt::QueuedConnection);
  410. int err = this->LocalLoop.exec();
  411. if (err != 0) {
  412. QMessageBox::critical(
  413. this, tr("Error"),
  414. tr("Error in generation process, project files may be invalid"),
  415. QMessageBox::Ok);
  416. }
  417. return 0 == err;
  418. }
  419. void CMakeSetupDialog::doGenerate()
  420. {
  421. if (this->CurrentState == Generating) {
  422. // stop generate
  423. doInterrupt();
  424. return;
  425. }
  426. // see if we need to configure
  427. // we'll need to configure if:
  428. // the configure step hasn't been done yet
  429. // generate was the last step done
  430. if (this->ConfigureNeeded) {
  431. if (!prepareConfigure()) {
  432. return;
  433. }
  434. }
  435. this->enterState(Generating);
  436. bool config_passed = true;
  437. if (this->ConfigureNeeded) {
  438. this->CacheValues->cacheModel()->setShowNewProperties(false);
  439. this->ProgressFactor = 0.5;
  440. config_passed = doConfigureInternal();
  441. this->ProgressOffset = 0.5;
  442. }
  443. if (config_passed) {
  444. doGenerateInternal();
  445. }
  446. this->ProgressOffset = 0.0;
  447. this->ProgressFactor = 1.0;
  448. this->CacheValues->cacheModel()->setShowNewProperties(true);
  449. this->enterState(ReadyConfigure);
  450. this->ProgressBar->reset();
  451. #ifdef QT_WINEXTRAS
  452. this->TaskbarButton->progress()->reset();
  453. #endif
  454. this->ConfigureNeeded = true;
  455. }
  456. void CMakeSetupDialog::doOpenProject()
  457. {
  458. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "open",
  459. Qt::QueuedConnection);
  460. }
  461. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  462. {
  463. // prompt for close if there are unsaved changes, and we're not busy
  464. if (this->CacheModified) {
  465. QString msg = tr("You have changed options but not rebuilt, "
  466. "are you sure you want to exit?");
  467. QString title = tr("Confirm Exit");
  468. QMessageBox::StandardButton btn;
  469. btn = QMessageBox::critical(this, title, msg,
  470. QMessageBox::Yes | QMessageBox::No);
  471. if (btn == QMessageBox::No) {
  472. e->ignore();
  473. }
  474. }
  475. // don't close if we're busy, unless the user really wants to
  476. if (this->CurrentState == Configuring) {
  477. QString msg =
  478. tr("You are in the middle of a Configure.\n"
  479. "If you Exit now the configure information will be lost.\n"
  480. "Are you sure you want to Exit?");
  481. QString title = tr("Confirm Exit");
  482. QMessageBox::StandardButton btn;
  483. btn = QMessageBox::critical(this, title, msg,
  484. QMessageBox::Yes | QMessageBox::No);
  485. if (btn == QMessageBox::No) {
  486. e->ignore();
  487. } else {
  488. this->doInterrupt();
  489. }
  490. }
  491. // let the generate finish
  492. if (this->CurrentState == Generating) {
  493. e->ignore();
  494. }
  495. }
  496. void CMakeSetupDialog::doHelp()
  497. {
  498. QString msg = tr(
  499. "CMake is used to configure and generate build files for "
  500. "software projects. The basic steps for configuring a project are as "
  501. "follows:\r\n\r\n1. Select the source directory for the project. This "
  502. "should "
  503. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the "
  504. "build "
  505. "directory for the project. This is the directory where the project "
  506. "will be "
  507. "built. It can be the same or a different directory than the source "
  508. "directory. For easy clean up, a separate build directory is "
  509. "recommended. "
  510. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the "
  511. "source and binary directories are selected, it is time to press the "
  512. "Configure button. This will cause CMake to read all of the input files "
  513. "and "
  514. "discover all the variables used by the project. The first time a "
  515. "variable "
  516. "is displayed it will be in Red. Users should inspect red variables "
  517. "making "
  518. "sure the values are correct. For some projects the Configure process "
  519. "can "
  520. "be iterative, so continue to press the Configure button until there are "
  521. "no "
  522. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you "
  523. "should click the Generate button. This will write the build files to "
  524. "the build "
  525. "directory.");
  526. QDialog dialog;
  527. QFontMetrics met(this->font());
  528. #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
  529. int msgWidth = met.horizontalAdvance(msg);
  530. #else
  531. int msgWidth = met.width(msg);
  532. #endif
  533. dialog.setMinimumSize(msgWidth / 15, 20);
  534. dialog.setWindowTitle(tr("Help"));
  535. QVBoxLayout* l = new QVBoxLayout(&dialog);
  536. QLabel* lab = new QLabel(&dialog);
  537. lab->setText(msg);
  538. lab->setWordWrap(true);
  539. QDialogButtonBox* btns =
  540. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  541. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  542. &QDialog::accept);
  543. l->addWidget(lab);
  544. l->addWidget(btns);
  545. dialog.exec();
  546. }
  547. void CMakeSetupDialog::doInterrupt()
  548. {
  549. this->enterState(Interrupting);
  550. this->CMakeThread->cmakeInstance()->interrupt();
  551. }
  552. void CMakeSetupDialog::doSourceBrowse()
  553. {
  554. QString dir = QFileDialog::getExistingDirectory(
  555. this, tr("Enter Path to Source"), this->SourceDirectory->text(),
  556. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  557. if (!dir.isEmpty()) {
  558. this->setSourceDirectory(dir);
  559. }
  560. }
  561. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  562. {
  563. if (this->SourceDirectory->text() != dir) {
  564. this->SourceDirectory->blockSignals(true);
  565. this->SourceDirectory->setText(dir);
  566. this->SourceDirectory->blockSignals(false);
  567. }
  568. }
  569. void CMakeSetupDialog::updateBinaryDirectory(const QString& dir)
  570. {
  571. if (this->BinaryDirectory->currentText() != dir) {
  572. this->BinaryDirectory->blockSignals(true);
  573. this->BinaryDirectory->setEditText(dir);
  574. this->BinaryDirectory->blockSignals(false);
  575. }
  576. }
  577. void CMakeSetupDialog::doBinaryBrowse()
  578. {
  579. QString dir = QFileDialog::getExistingDirectory(
  580. this, tr("Enter Path to Build"), this->BinaryDirectory->currentText(),
  581. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  582. if (!dir.isEmpty() && dir != this->BinaryDirectory->currentText()) {
  583. this->setBinaryDirectory(dir);
  584. }
  585. }
  586. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  587. {
  588. this->BinaryDirectory->setEditText(dir);
  589. }
  590. void CMakeSetupDialog::onSourceDirectoryChanged(const QString& dir)
  591. {
  592. this->Output->clear();
  593. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  594. "setSourceDirectory", Qt::QueuedConnection,
  595. Q_ARG(QString, dir));
  596. }
  597. void CMakeSetupDialog::onBinaryDirectoryChanged(const QString& dir)
  598. {
  599. QString title = QString(tr("CMake %1 - %2"));
  600. title = title.arg(cmVersion::GetCMakeVersion());
  601. title = title.arg(dir);
  602. this->setWindowTitle(title);
  603. this->CacheModified = false;
  604. this->CacheValues->cacheModel()->clear();
  605. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())
  606. ->clearChanges();
  607. this->Output->clear();
  608. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  609. "setBinaryDirectory", Qt::QueuedConnection,
  610. Q_ARG(QString, dir));
  611. }
  612. void CMakeSetupDialog::setSourceDirectory(const QString& dir)
  613. {
  614. this->SourceDirectory->setText(dir);
  615. }
  616. void CMakeSetupDialog::showProgress(const QString& /*msg*/, float percent)
  617. {
  618. percent = (percent * ProgressFactor) + ProgressOffset;
  619. this->ProgressBar->setValue(qRound(percent * 100));
  620. #ifdef QT_WINEXTRAS
  621. QWinTaskbarProgress* progress = this->TaskbarButton->progress();
  622. progress->setVisible(true);
  623. progress->setValue(qRound(percent * 100));
  624. #endif
  625. }
  626. void CMakeSetupDialog::error(const QString& msg)
  627. {
  628. this->Output->setCurrentCharFormat(this->ErrorFormat);
  629. // QTextEdit will terminate the msg with a ParagraphSeparator, but it also
  630. // replaces
  631. // all newlines with ParagraphSeparators. By replacing the newlines by
  632. // ourself, one
  633. // error msg will be one paragraph.
  634. QString paragraph(msg);
  635. paragraph.replace(QLatin1Char('\n'), QChar::LineSeparator);
  636. this->Output->append(paragraph);
  637. }
  638. void CMakeSetupDialog::message(const QString& msg)
  639. {
  640. this->Output->setCurrentCharFormat(this->MessageFormat);
  641. this->Output->append(msg);
  642. }
  643. void CMakeSetupDialog::setEnabledState(bool enabled)
  644. {
  645. // disable parts of the GUI during configure/generate
  646. this->CacheValues->cacheModel()->setEditEnabled(enabled);
  647. this->SourceDirectory->setEnabled(enabled);
  648. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  649. this->BinaryDirectory->setEnabled(enabled);
  650. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  651. this->ReloadCacheAction->setEnabled(enabled);
  652. this->DeleteCacheAction->setEnabled(enabled);
  653. this->ExitAction->setEnabled(enabled);
  654. this->ConfigureAction->setEnabled(enabled);
  655. this->AddEntry->setEnabled(enabled);
  656. this->RemoveEntry->setEnabled(false); // let selection re-enable it
  657. this->Environment->setEnabled(enabled);
  658. }
  659. bool CMakeSetupDialog::setupFirstConfigure()
  660. {
  661. FirstConfigure dialog;
  662. // initialize dialog and restore saved settings
  663. // add generators
  664. dialog.setGenerators(
  665. this->CMakeThread->cmakeInstance()->availableGenerators());
  666. // restore from settings
  667. dialog.loadFromSettings();
  668. if (dialog.exec() == QDialog::Accepted) {
  669. dialog.saveToSettings();
  670. this->CMakeThread->cmakeInstance()->setGenerator(dialog.getGenerator());
  671. this->CMakeThread->cmakeInstance()->setPlatform(dialog.getPlatform());
  672. this->CMakeThread->cmakeInstance()->setToolset(dialog.getToolset());
  673. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  674. if (dialog.compilerSetup()) {
  675. QString fortranCompiler = dialog.getFortranCompiler();
  676. if (!fortranCompiler.isEmpty()) {
  677. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  678. "Fortran compiler.", fortranCompiler, false);
  679. }
  680. QString cxxCompiler = dialog.getCXXCompiler();
  681. if (!cxxCompiler.isEmpty()) {
  682. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  683. "CXX compiler.", cxxCompiler, false);
  684. }
  685. QString cCompiler = dialog.getCCompiler();
  686. if (!cCompiler.isEmpty()) {
  687. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  688. "C compiler.", cCompiler, false);
  689. }
  690. } else if (dialog.crossCompilerSetup()) {
  691. QString fortranCompiler = dialog.getFortranCompiler();
  692. if (!fortranCompiler.isEmpty()) {
  693. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  694. "Fortran compiler.", fortranCompiler, false);
  695. }
  696. QString mode = dialog.getCrossIncludeMode();
  697. m->insertProperty(QCMakeProperty::STRING,
  698. "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE",
  699. tr("CMake Find Include Mode"), mode, false);
  700. mode = dialog.getCrossLibraryMode();
  701. m->insertProperty(QCMakeProperty::STRING,
  702. "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY",
  703. tr("CMake Find Library Mode"), mode, false);
  704. mode = dialog.getCrossProgramMode();
  705. m->insertProperty(QCMakeProperty::STRING,
  706. "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM",
  707. tr("CMake Find Program Mode"), mode, false);
  708. QString rootPath = dialog.getCrossRoot();
  709. m->insertProperty(QCMakeProperty::PATH, "CMAKE_FIND_ROOT_PATH",
  710. tr("CMake Find Root Path"), rootPath, false);
  711. QString systemName = dialog.getSystemName();
  712. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_NAME",
  713. tr("CMake System Name"), systemName, false);
  714. QString systemVersion = dialog.getSystemVersion();
  715. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_VERSION",
  716. tr("CMake System Version"), systemVersion, false);
  717. QString systemProcessor = dialog.getSystemProcessor();
  718. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_PROCESSOR",
  719. tr("CMake System Processor"), systemProcessor, false);
  720. QString cxxCompiler = dialog.getCXXCompiler();
  721. if (!cxxCompiler.isEmpty()) {
  722. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  723. tr("CXX compiler."), cxxCompiler, false);
  724. }
  725. QString cCompiler = dialog.getCCompiler();
  726. if (!cCompiler.isEmpty()) {
  727. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  728. tr("C compiler."), cCompiler, false);
  729. }
  730. } else if (dialog.crossCompilerToolChainFile()) {
  731. QString toolchainFile = dialog.getCrossCompilerToolChainFile();
  732. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_TOOLCHAIN_FILE",
  733. tr("Cross Compile ToolChain File"), toolchainFile,
  734. false);
  735. }
  736. return true;
  737. }
  738. return false;
  739. }
  740. void CMakeSetupDialog::updateGeneratorLabel(const QString& gen)
  741. {
  742. QString str = tr("Current Generator: ");
  743. if (gen.isEmpty()) {
  744. str += tr("None");
  745. } else {
  746. str += gen;
  747. }
  748. this->Generator->setText(str);
  749. }
  750. void CMakeSetupDialog::doReloadCache()
  751. {
  752. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "reloadCache",
  753. Qt::QueuedConnection);
  754. }
  755. void CMakeSetupDialog::doDeleteCache()
  756. {
  757. QString title = tr("Delete Cache");
  758. QString msg = tr("Are you sure you want to delete the cache?");
  759. QMessageBox::StandardButton btn;
  760. btn = QMessageBox::information(this, title, msg,
  761. QMessageBox::Yes | QMessageBox::No);
  762. if (btn == QMessageBox::No) {
  763. return;
  764. }
  765. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "deleteCache",
  766. Qt::QueuedConnection);
  767. }
  768. void CMakeSetupDialog::doAbout()
  769. {
  770. QString msg = tr(
  771. "CMake %1 (cmake.org).\n"
  772. "CMake suite maintained and supported by Kitware (kitware.com/cmake).\n"
  773. "Distributed under terms of the BSD 3-Clause License.\n"
  774. "\n"
  775. "CMake GUI maintained by csimsoft,\n"
  776. "built using Qt %2 (qt-project.org).\n"
  777. #ifdef USE_LGPL
  778. "\n"
  779. "The Qt Toolkit is Copyright (C) The Qt Company Ltd.\n"
  780. "Qt is licensed under terms of the GNU LGPLv" USE_LGPL ", available at:\n"
  781. " \"%3\""
  782. #endif
  783. );
  784. msg = msg.arg(cmVersion::GetCMakeVersion());
  785. msg = msg.arg(qVersion());
  786. #ifdef USE_LGPL
  787. std::string lgpl =
  788. cmSystemTools::GetCMakeRoot() + "/Licenses/LGPLv" USE_LGPL ".txt";
  789. msg = msg.arg(lgpl.c_str());
  790. #endif
  791. QDialog dialog;
  792. dialog.setWindowTitle(tr("About"));
  793. QVBoxLayout* l = new QVBoxLayout(&dialog);
  794. QLabel* lab = new QLabel(&dialog);
  795. l->addWidget(lab);
  796. lab->setText(msg);
  797. lab->setWordWrap(true);
  798. QDialogButtonBox* btns =
  799. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  800. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  801. &QDialog::accept);
  802. l->addWidget(btns);
  803. dialog.exec();
  804. }
  805. void CMakeSetupDialog::setExitAfterGenerate(bool b)
  806. {
  807. this->ExitAfterGenerate = b;
  808. }
  809. void CMakeSetupDialog::addBinaryPath(const QString& path)
  810. {
  811. QString cleanpath = QDir::cleanPath(path);
  812. // update UI
  813. this->BinaryDirectory->blockSignals(true);
  814. int idx = this->BinaryDirectory->findText(cleanpath);
  815. if (idx != -1) {
  816. this->BinaryDirectory->removeItem(idx);
  817. }
  818. this->BinaryDirectory->insertItem(0, cleanpath);
  819. this->BinaryDirectory->setCurrentIndex(0);
  820. this->BinaryDirectory->blockSignals(false);
  821. // save to registry
  822. QStringList buildPaths = this->loadBuildPaths();
  823. buildPaths.removeAll(cleanpath);
  824. buildPaths.prepend(cleanpath);
  825. this->saveBuildPaths(buildPaths);
  826. }
  827. void CMakeSetupDialog::dragEnterEvent(QDragEnterEvent* e)
  828. {
  829. if (!(this->CurrentState == ReadyConfigure ||
  830. this->CurrentState == ReadyGenerate)) {
  831. e->ignore();
  832. return;
  833. }
  834. const QMimeData* dat = e->mimeData();
  835. QList<QUrl> urls = dat->urls();
  836. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  837. if (!file.isEmpty() &&
  838. (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive) ||
  839. file.endsWith("CMakeLists.txt", Qt::CaseInsensitive))) {
  840. e->accept();
  841. } else {
  842. e->ignore();
  843. }
  844. }
  845. void CMakeSetupDialog::dropEvent(QDropEvent* e)
  846. {
  847. if (!(this->CurrentState == ReadyConfigure ||
  848. this->CurrentState == ReadyGenerate)) {
  849. return;
  850. }
  851. const QMimeData* dat = e->mimeData();
  852. QList<QUrl> urls = dat->urls();
  853. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  854. if (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive)) {
  855. QFileInfo info(file);
  856. if (this->CMakeThread->cmakeInstance()->binaryDirectory() !=
  857. info.absolutePath()) {
  858. this->setBinaryDirectory(info.absolutePath());
  859. }
  860. } else if (file.endsWith("CMakeLists.txt", Qt::CaseInsensitive)) {
  861. QFileInfo info(file);
  862. if (this->CMakeThread->cmakeInstance()->binaryDirectory() !=
  863. info.absolutePath()) {
  864. this->setSourceDirectory(info.absolutePath());
  865. this->setBinaryDirectory(info.absolutePath());
  866. }
  867. }
  868. }
  869. QStringList CMakeSetupDialog::loadBuildPaths()
  870. {
  871. QSettings settings;
  872. settings.beginGroup("Settings/StartPath");
  873. QStringList buildPaths;
  874. for (int i = 0; i < 10; i++) {
  875. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  876. if (!p.isEmpty()) {
  877. buildPaths.append(p);
  878. }
  879. }
  880. return buildPaths;
  881. }
  882. void CMakeSetupDialog::saveBuildPaths(const QStringList& paths)
  883. {
  884. QSettings settings;
  885. settings.beginGroup("Settings/StartPath");
  886. int num = paths.count();
  887. if (num > 10) {
  888. num = 10;
  889. }
  890. for (int i = 0; i < num; i++) {
  891. settings.setValue(QString("WhereBuild%1").arg(i), paths[i]);
  892. }
  893. }
  894. void CMakeSetupDialog::setCacheModified()
  895. {
  896. this->CacheModified = true;
  897. this->ConfigureNeeded = true;
  898. this->enterState(ReadyConfigure);
  899. }
  900. void CMakeSetupDialog::removeSelectedCacheEntries()
  901. {
  902. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  903. QList<QPersistentModelIndex> pidxs;
  904. foreach (QModelIndex const& i, idxs) {
  905. pidxs.append(i);
  906. }
  907. foreach (QPersistentModelIndex const& pi, pidxs) {
  908. this->CacheValues->model()->removeRow(pi.row(), pi.parent());
  909. }
  910. }
  911. void CMakeSetupDialog::selectionChanged()
  912. {
  913. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  914. if (idxs.count() &&
  915. (this->CurrentState == ReadyConfigure ||
  916. this->CurrentState == ReadyGenerate)) {
  917. this->RemoveEntry->setEnabled(true);
  918. } else {
  919. this->RemoveEntry->setEnabled(false);
  920. }
  921. }
  922. void CMakeSetupDialog::enterState(CMakeSetupDialog::State s)
  923. {
  924. if (s == this->CurrentState) {
  925. return;
  926. }
  927. this->CurrentState = s;
  928. if (s == Interrupting) {
  929. this->ConfigureButton->setEnabled(false);
  930. this->GenerateButton->setEnabled(false);
  931. this->OpenProjectButton->setEnabled(false);
  932. } else if (s == Configuring) {
  933. this->setEnabledState(false);
  934. this->GenerateButton->setEnabled(false);
  935. this->GenerateAction->setEnabled(false);
  936. this->OpenProjectButton->setEnabled(false);
  937. this->ConfigureButton->setText(tr("&Stop"));
  938. } else if (s == Generating) {
  939. this->CacheModified = false;
  940. this->setEnabledState(false);
  941. this->ConfigureButton->setEnabled(false);
  942. this->GenerateAction->setEnabled(false);
  943. this->OpenProjectButton->setEnabled(false);
  944. this->GenerateButton->setText(tr("&Stop"));
  945. } else if (s == ReadyConfigure || s == ReadyGenerate) {
  946. this->setEnabledState(true);
  947. this->GenerateButton->setEnabled(true);
  948. this->GenerateAction->setEnabled(true);
  949. this->ConfigureButton->setEnabled(true);
  950. this->ConfigureButton->setText(tr("&Configure"));
  951. this->GenerateButton->setText(tr("&Generate"));
  952. }
  953. }
  954. void CMakeSetupDialog::editEnvironment()
  955. {
  956. EnvironmentDialog dialog(this->CMakeThread->cmakeInstance()->environment(),
  957. this);
  958. if (dialog.exec() == QDialog::Accepted) {
  959. QMetaObject::invokeMethod(
  960. this->CMakeThread->cmakeInstance(), "setEnvironment",
  961. Q_ARG(QProcessEnvironment, dialog.environment()));
  962. }
  963. }
  964. void CMakeSetupDialog::addCacheEntry()
  965. {
  966. QDialog dialog(this);
  967. dialog.resize(400, 200);
  968. dialog.setWindowTitle(tr("Add Cache Entry"));
  969. QVBoxLayout* l = new QVBoxLayout(&dialog);
  970. AddCacheEntry* w =
  971. new AddCacheEntry(&dialog, this->AddVariableNames, this->AddVariableTypes);
  972. QDialogButtonBox* btns = new QDialogButtonBox(
  973. QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
  974. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  975. &QDialog::accept);
  976. QObject::connect(btns, &QDialogButtonBox::rejected, &dialog,
  977. &QDialog::reject);
  978. l->addWidget(w);
  979. l->addStretch();
  980. l->addWidget(btns);
  981. if (QDialog::Accepted == dialog.exec()) {
  982. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  983. m->insertProperty(w->type(), w->name(), w->description(), w->value(),
  984. false);
  985. // only add variable names to the completion which are new
  986. if (!this->AddVariableNames.contains(w->name())) {
  987. this->AddVariableNames << w->name();
  988. this->AddVariableTypes << w->typeString();
  989. // limit to at most 100 completion items
  990. if (this->AddVariableNames.size() > 100) {
  991. this->AddVariableNames.removeFirst();
  992. this->AddVariableTypes.removeFirst();
  993. }
  994. // make sure CMAKE_INSTALL_PREFIX is always there
  995. if (!this->AddVariableNames.contains("CMAKE_INSTALL_PREFIX")) {
  996. this->AddVariableNames << "CMAKE_INSTALL_PREFIX";
  997. this->AddVariableTypes << "PATH";
  998. }
  999. QSettings settings;
  1000. settings.beginGroup("Settings/StartPath");
  1001. settings.setValue("AddVariableNames", this->AddVariableNames);
  1002. settings.setValue("AddVariableTypes", this->AddVariableTypes);
  1003. }
  1004. }
  1005. }
  1006. void CMakeSetupDialog::startSearch()
  1007. {
  1008. this->Search->setFocus(Qt::OtherFocusReason);
  1009. this->Search->selectAll();
  1010. }
  1011. void CMakeSetupDialog::setDebugOutput(bool flag)
  1012. {
  1013. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  1014. "setDebugOutput", Qt::QueuedConnection,
  1015. Q_ARG(bool, flag));
  1016. }
  1017. void CMakeSetupDialog::setGroupedView(bool v)
  1018. {
  1019. this->CacheValues->cacheModel()->setViewType(v ? QCMakeCacheModel::GroupView
  1020. : QCMakeCacheModel::FlatView);
  1021. this->CacheValues->setRootIsDecorated(v);
  1022. QSettings settings;
  1023. settings.beginGroup("Settings/StartPath");
  1024. settings.setValue("GroupView", v);
  1025. }
  1026. void CMakeSetupDialog::setAdvancedView(bool v)
  1027. {
  1028. this->CacheValues->setShowAdvanced(v);
  1029. QSettings settings;
  1030. settings.beginGroup("Settings/StartPath");
  1031. settings.setValue("AdvancedView", v);
  1032. }
  1033. void CMakeSetupDialog::showUserChanges()
  1034. {
  1035. QSet<QCMakeProperty> changes =
  1036. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())
  1037. ->changes();
  1038. QDialog dialog(this);
  1039. dialog.setWindowTitle(tr("My Changes"));
  1040. dialog.resize(600, 400);
  1041. QVBoxLayout* l = new QVBoxLayout(&dialog);
  1042. QTextEdit* textedit = new QTextEdit(&dialog);
  1043. textedit->setReadOnly(true);
  1044. l->addWidget(textedit);
  1045. QDialogButtonBox* btns =
  1046. new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, &dialog);
  1047. QObject::connect(btns, &QDialogButtonBox::rejected, &dialog,
  1048. &QDialog::accept);
  1049. l->addWidget(btns);
  1050. QString command;
  1051. QString cache;
  1052. foreach (QCMakeProperty const& prop, changes) {
  1053. QString type;
  1054. switch (prop.Type) {
  1055. case QCMakeProperty::BOOL:
  1056. type = "BOOL";
  1057. break;
  1058. case QCMakeProperty::PATH:
  1059. type = "PATH";
  1060. break;
  1061. case QCMakeProperty::FILEPATH:
  1062. type = "FILEPATH";
  1063. break;
  1064. case QCMakeProperty::STRING:
  1065. type = "STRING";
  1066. break;
  1067. }
  1068. QString value;
  1069. if (prop.Type == QCMakeProperty::BOOL) {
  1070. value = prop.Value.toBool() ? "1" : "0";
  1071. } else {
  1072. value = prop.Value.toString();
  1073. }
  1074. QString const line = QString("%1:%2=").arg(prop.Key, type);
  1075. command += QString("-D%1\"%2\" ").arg(line, value);
  1076. cache += QString("%1%2\n").arg(line, value);
  1077. }
  1078. textedit->append(tr("Commandline options:"));
  1079. textedit->append(command);
  1080. textedit->append("\n");
  1081. textedit->append(tr("Cache file:"));
  1082. textedit->append(cache);
  1083. dialog.exec();
  1084. }
  1085. void CMakeSetupDialog::setSearchFilter(const QString& str)
  1086. {
  1087. this->CacheValues->selectionModel()->clear();
  1088. this->CacheValues->setSearchFilter(str);
  1089. }
  1090. void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
  1091. {
  1092. std::unique_ptr<QMenu> menu(this->Output->createStandardContextMenu());
  1093. menu->addSeparator();
  1094. auto* a = menu->addAction(tr("Find..."));
  1095. QObject::connect(a, &QAction::triggered, this,
  1096. &CMakeSetupDialog::doOutputFindDialog);
  1097. a->setShortcut(QKeySequence::Find);
  1098. a = menu->addAction(tr("Find Next"));
  1099. QObject::connect(a, &QAction::triggered, this,
  1100. &CMakeSetupDialog::doOutputFindNext);
  1101. a->setShortcut(QKeySequence::FindNext);
  1102. a = menu->addAction(tr("Find Previous"));
  1103. QObject::connect(a, &QAction::triggered, this,
  1104. &CMakeSetupDialog::doOutputFindPrev);
  1105. a->setShortcut(QKeySequence::FindPrevious);
  1106. menu->addSeparator();
  1107. a = menu->addAction(tr("Goto Next Error"));
  1108. QObject::connect(a, &QAction::triggered, this,
  1109. &CMakeSetupDialog::doOutputErrorNext);
  1110. a->setShortcut(QKeySequence(Qt::Key_F8));
  1111. menu->exec(this->Output->mapToGlobal(pt));
  1112. }
  1113. void CMakeSetupDialog::doOutputFindDialog()
  1114. {
  1115. QStringList strings(this->FindHistory);
  1116. QString selection = this->Output->textCursor().selectedText();
  1117. if (!selection.isEmpty() && !selection.contains(QChar::ParagraphSeparator) &&
  1118. !selection.contains(QChar::LineSeparator)) {
  1119. strings.push_front(selection);
  1120. }
  1121. bool ok;
  1122. QString search = QInputDialog::getItem(this, tr("Find in Output"),
  1123. tr("Find:"), strings, 0, true, &ok);
  1124. if (ok && !search.isEmpty()) {
  1125. if (!this->FindHistory.contains(search)) {
  1126. this->FindHistory.push_front(search);
  1127. }
  1128. doOutputFindNext();
  1129. }
  1130. }
  1131. void CMakeSetupDialog::doRegexExplorerDialog()
  1132. {
  1133. RegexExplorer dialog(this);
  1134. dialog.exec();
  1135. }
  1136. void CMakeSetupDialog::doOutputFindPrev()
  1137. {
  1138. doOutputFindNext(false);
  1139. }
  1140. void CMakeSetupDialog::doOutputFindNext(bool directionForward)
  1141. {
  1142. if (this->FindHistory.isEmpty()) {
  1143. doOutputFindDialog(); // will re-call this function again
  1144. return;
  1145. }
  1146. QString search = this->FindHistory.front();
  1147. QTextCursor textCursor = this->Output->textCursor();
  1148. QTextDocument* document = this->Output->document();
  1149. QTextDocument::FindFlags flags;
  1150. if (!directionForward) {
  1151. flags |= QTextDocument::FindBackward;
  1152. }
  1153. textCursor = document->find(search, textCursor, flags);
  1154. if (textCursor.isNull()) {
  1155. // first search found nothing, wrap around and search again
  1156. textCursor = this->Output->textCursor();
  1157. textCursor.movePosition(directionForward ? QTextCursor::Start
  1158. : QTextCursor::End);
  1159. textCursor = document->find(search, textCursor, flags);
  1160. }
  1161. if (textCursor.hasSelection()) {
  1162. this->Output->setTextCursor(textCursor);
  1163. }
  1164. }
  1165. void CMakeSetupDialog::doOutputErrorNext()
  1166. {
  1167. QTextCursor textCursor = this->Output->textCursor();
  1168. bool atEnd = false;
  1169. // move cursor out of current error-block
  1170. if (textCursor.blockCharFormat() == this->ErrorFormat) {
  1171. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1172. }
  1173. // move cursor to next error-block
  1174. while (textCursor.blockCharFormat() != this->ErrorFormat && !atEnd) {
  1175. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1176. }
  1177. if (atEnd) {
  1178. // first search found nothing, wrap around and search again
  1179. atEnd = !textCursor.movePosition(QTextCursor::Start);
  1180. // move cursor to next error-block
  1181. while (textCursor.blockCharFormat() != this->ErrorFormat && !atEnd) {
  1182. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1183. }
  1184. }
  1185. if (!atEnd) {
  1186. textCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
  1187. QTextCharFormat selectionFormat;
  1188. selectionFormat.setBackground(Qt::yellow);
  1189. QTextEdit::ExtraSelection extraSelection = { textCursor, selectionFormat };
  1190. this->Output->setExtraSelections(QList<QTextEdit::ExtraSelection>()
  1191. << extraSelection);
  1192. // make the whole error-block visible
  1193. this->Output->setTextCursor(textCursor);
  1194. // remove the selection to see the extraSelection
  1195. textCursor.setPosition(textCursor.anchor());
  1196. this->Output->setTextCursor(textCursor);
  1197. }
  1198. }
  1199. void CMakeSetupDialog::doWarningMessagesDialog()
  1200. {
  1201. WarningMessagesDialog dialog(this, this->CMakeThread->cmakeInstance());
  1202. dialog.exec();
  1203. }