CMakeSetupDialog.cxx 49 KB

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