CMakeSetupDialog.cxx 49 KB

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