CMakeSetupDialog.cxx 48 KB

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