qconf.cc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <[email protected]>
  4. * Copyright (C) 2015 Boris Barbulovski <[email protected]>
  5. */
  6. #include <QAction>
  7. #include <QActionGroup>
  8. #include <QApplication>
  9. #include <QCloseEvent>
  10. #include <QDebug>
  11. #include <QFileDialog>
  12. #include <QLabel>
  13. #include <QLayout>
  14. #include <QList>
  15. #include <QMenu>
  16. #include <QMenuBar>
  17. #include <QMessageBox>
  18. #include <QRegularExpression>
  19. #include <QScreen>
  20. #include <QToolBar>
  21. #include <stdlib.h>
  22. #include "lkc.h"
  23. #include "qconf.h"
  24. #include "images.h"
  25. static QApplication *configApp;
  26. static ConfigSettings *configSettings;
  27. QAction *ConfigMainWindow::saveAction;
  28. ConfigSettings::ConfigSettings()
  29. : QSettings("kernel.org", "qconf")
  30. {
  31. }
  32. /**
  33. * Reads a list of integer values from the application settings.
  34. */
  35. QList<int> ConfigSettings::readSizes(const QString& key, bool *ok)
  36. {
  37. QList<int> result;
  38. if (contains(key))
  39. {
  40. QStringList entryList = value(key).toStringList();
  41. QStringList::Iterator it;
  42. for (it = entryList.begin(); it != entryList.end(); ++it)
  43. result.push_back((*it).toInt());
  44. *ok = true;
  45. }
  46. else
  47. *ok = false;
  48. return result;
  49. }
  50. /**
  51. * Writes a list of integer values to the application settings.
  52. */
  53. bool ConfigSettings::writeSizes(const QString& key, const QList<int>& value)
  54. {
  55. QStringList stringList;
  56. QList<int>::ConstIterator it;
  57. for (it = value.begin(); it != value.end(); ++it)
  58. stringList.push_back(QString::number(*it));
  59. setValue(key, stringList);
  60. return true;
  61. }
  62. QIcon ConfigItem::symbolYesIcon;
  63. QIcon ConfigItem::symbolModIcon;
  64. QIcon ConfigItem::symbolNoIcon;
  65. QIcon ConfigItem::choiceYesIcon;
  66. QIcon ConfigItem::choiceNoIcon;
  67. QIcon ConfigItem::menuIcon;
  68. QIcon ConfigItem::menubackIcon;
  69. /*
  70. * update the displayed of a menu entry
  71. */
  72. void ConfigItem::updateMenu(void)
  73. {
  74. ConfigList* list;
  75. struct symbol* sym;
  76. struct property *prop;
  77. QString prompt;
  78. int type;
  79. tristate expr;
  80. list = listView();
  81. if (goParent) {
  82. setIcon(promptColIdx, menubackIcon);
  83. prompt = "..";
  84. goto set_prompt;
  85. }
  86. sym = menu->sym;
  87. prop = menu->prompt;
  88. prompt = menu_get_prompt(menu);
  89. if (prop) switch (prop->type) {
  90. case P_MENU:
  91. if (list->mode == singleMode || list->mode == symbolMode) {
  92. /* a menuconfig entry is displayed differently
  93. * depending whether it's at the view root or a child.
  94. */
  95. if (sym && list->rootEntry == menu)
  96. break;
  97. setIcon(promptColIdx, menuIcon);
  98. } else {
  99. if (sym)
  100. break;
  101. setIcon(promptColIdx, QIcon());
  102. }
  103. goto set_prompt;
  104. case P_COMMENT:
  105. setIcon(promptColIdx, QIcon());
  106. prompt = "*** " + prompt + " ***";
  107. goto set_prompt;
  108. default:
  109. ;
  110. }
  111. if (!sym)
  112. goto set_prompt;
  113. setText(nameColIdx, sym->name);
  114. type = sym_get_type(sym);
  115. switch (type) {
  116. case S_BOOLEAN:
  117. case S_TRISTATE:
  118. char ch;
  119. if (!sym_is_changeable(sym) && list->optMode == normalOpt) {
  120. setIcon(promptColIdx, QIcon());
  121. break;
  122. }
  123. expr = sym_get_tristate_value(sym);
  124. switch (expr) {
  125. case yes:
  126. if (sym_is_choice_value(sym) && type == S_BOOLEAN)
  127. setIcon(promptColIdx, choiceYesIcon);
  128. else
  129. setIcon(promptColIdx, symbolYesIcon);
  130. ch = 'Y';
  131. break;
  132. case mod:
  133. setIcon(promptColIdx, symbolModIcon);
  134. ch = 'M';
  135. break;
  136. default:
  137. if (sym_is_choice_value(sym) && type == S_BOOLEAN)
  138. setIcon(promptColIdx, choiceNoIcon);
  139. else
  140. setIcon(promptColIdx, symbolNoIcon);
  141. ch = 'N';
  142. break;
  143. }
  144. setText(dataColIdx, QChar(ch));
  145. break;
  146. case S_INT:
  147. case S_HEX:
  148. case S_STRING:
  149. setText(dataColIdx, sym_get_string_value(sym));
  150. break;
  151. }
  152. if (!sym_has_value(sym) && visible)
  153. prompt += " (NEW)";
  154. set_prompt:
  155. setText(promptColIdx, prompt);
  156. }
  157. void ConfigItem::testUpdateMenu(bool v)
  158. {
  159. ConfigItem* i;
  160. visible = v;
  161. if (!menu)
  162. return;
  163. sym_calc_value(menu->sym);
  164. if (menu->flags & MENU_CHANGED) {
  165. /* the menu entry changed, so update all list items */
  166. menu->flags &= ~MENU_CHANGED;
  167. for (i = (ConfigItem*)menu->data; i; i = i->nextItem)
  168. i->updateMenu();
  169. } else if (listView()->updateAll)
  170. updateMenu();
  171. }
  172. /*
  173. * construct a menu entry
  174. */
  175. void ConfigItem::init(void)
  176. {
  177. if (menu) {
  178. ConfigList* list = listView();
  179. nextItem = (ConfigItem*)menu->data;
  180. menu->data = this;
  181. if (list->mode != fullMode)
  182. setExpanded(true);
  183. sym_calc_value(menu->sym);
  184. if (menu->sym) {
  185. enum symbol_type type = menu->sym->type;
  186. // Allow to edit "int", "hex", and "string" in-place in
  187. // the data column. Unfortunately, you cannot specify
  188. // the flags per column. Set ItemIsEditable for all
  189. // columns here, and check the column in createEditor().
  190. if (type == S_INT || type == S_HEX || type == S_STRING)
  191. setFlags(flags() | Qt::ItemIsEditable);
  192. }
  193. }
  194. updateMenu();
  195. }
  196. /*
  197. * destruct a menu entry
  198. */
  199. ConfigItem::~ConfigItem(void)
  200. {
  201. if (menu) {
  202. ConfigItem** ip = (ConfigItem**)&menu->data;
  203. for (; *ip; ip = &(*ip)->nextItem) {
  204. if (*ip == this) {
  205. *ip = nextItem;
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. QWidget *ConfigItemDelegate::createEditor(QWidget *parent,
  212. const QStyleOptionViewItem &option,
  213. const QModelIndex &index) const
  214. {
  215. ConfigItem *item;
  216. // Only the data column is editable
  217. if (index.column() != dataColIdx)
  218. return nullptr;
  219. // You cannot edit invisible menus
  220. item = static_cast<ConfigItem *>(index.internalPointer());
  221. if (!item || !item->menu || !menu_is_visible(item->menu))
  222. return nullptr;
  223. return QStyledItemDelegate::createEditor(parent, option, index);
  224. }
  225. void ConfigItemDelegate::setModelData(QWidget *editor,
  226. QAbstractItemModel *model,
  227. const QModelIndex &index) const
  228. {
  229. QLineEdit *lineEdit;
  230. ConfigItem *item;
  231. struct symbol *sym;
  232. bool success;
  233. lineEdit = qobject_cast<QLineEdit *>(editor);
  234. // If this is not a QLineEdit, use the parent's default.
  235. // (does this happen?)
  236. if (!lineEdit)
  237. goto parent;
  238. item = static_cast<ConfigItem *>(index.internalPointer());
  239. if (!item || !item->menu)
  240. goto parent;
  241. sym = item->menu->sym;
  242. if (!sym)
  243. goto parent;
  244. success = sym_set_string_value(sym, lineEdit->text().toUtf8().data());
  245. if (success) {
  246. ConfigList::updateListForAll();
  247. } else {
  248. QMessageBox::information(editor, "qconf",
  249. "Cannot set the data (maybe due to out of range).\n"
  250. "Setting the old value.");
  251. lineEdit->setText(sym_get_string_value(sym));
  252. }
  253. parent:
  254. QStyledItemDelegate::setModelData(editor, model, index);
  255. }
  256. ConfigList::ConfigList(QWidget *parent, const char *name)
  257. : QTreeWidget(parent),
  258. updateAll(false),
  259. showName(false), mode(singleMode), optMode(normalOpt),
  260. rootEntry(0), headerPopup(0)
  261. {
  262. setObjectName(name);
  263. setSortingEnabled(false);
  264. setRootIsDecorated(true);
  265. setVerticalScrollMode(ScrollPerPixel);
  266. setHorizontalScrollMode(ScrollPerPixel);
  267. setHeaderLabels(QStringList() << "Option" << "Name" << "Value");
  268. connect(this, &ConfigList::itemSelectionChanged,
  269. this, &ConfigList::updateSelection);
  270. if (name) {
  271. configSettings->beginGroup(name);
  272. showName = configSettings->value("/showName", false).toBool();
  273. optMode = (enum optionMode)configSettings->value("/optionMode", 0).toInt();
  274. configSettings->endGroup();
  275. connect(configApp, &QApplication::aboutToQuit,
  276. this, &ConfigList::saveSettings);
  277. }
  278. showColumn(promptColIdx);
  279. setItemDelegate(new ConfigItemDelegate(this));
  280. allLists.append(this);
  281. reinit();
  282. }
  283. ConfigList::~ConfigList()
  284. {
  285. allLists.removeOne(this);
  286. }
  287. bool ConfigList::menuSkip(struct menu *menu)
  288. {
  289. if (optMode == normalOpt && menu_is_visible(menu))
  290. return false;
  291. if (optMode == promptOpt && menu_has_prompt(menu))
  292. return false;
  293. if (optMode == allOpt)
  294. return false;
  295. return true;
  296. }
  297. void ConfigList::reinit(void)
  298. {
  299. hideColumn(nameColIdx);
  300. if (showName)
  301. showColumn(nameColIdx);
  302. updateListAll();
  303. }
  304. void ConfigList::setOptionMode(QAction *action)
  305. {
  306. if (action == showNormalAction)
  307. optMode = normalOpt;
  308. else if (action == showAllAction)
  309. optMode = allOpt;
  310. else
  311. optMode = promptOpt;
  312. updateListAll();
  313. }
  314. void ConfigList::saveSettings(void)
  315. {
  316. if (!objectName().isEmpty()) {
  317. configSettings->beginGroup(objectName());
  318. configSettings->setValue("/showName", showName);
  319. configSettings->setValue("/optionMode", (int)optMode);
  320. configSettings->endGroup();
  321. }
  322. }
  323. ConfigItem* ConfigList::findConfigItem(struct menu *menu)
  324. {
  325. ConfigItem* item = (ConfigItem*)menu->data;
  326. for (; item; item = item->nextItem) {
  327. if (this == item->listView())
  328. break;
  329. }
  330. return item;
  331. }
  332. void ConfigList::updateSelection(void)
  333. {
  334. struct menu *menu;
  335. enum prop_type type;
  336. if (selectedItems().count() == 0)
  337. return;
  338. ConfigItem* item = (ConfigItem*)selectedItems().first();
  339. if (!item)
  340. return;
  341. menu = item->menu;
  342. emit menuChanged(menu);
  343. if (!menu)
  344. return;
  345. type = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  346. if (mode == menuMode && type == P_MENU)
  347. emit menuSelected(menu);
  348. }
  349. void ConfigList::updateList()
  350. {
  351. ConfigItem* last = 0;
  352. ConfigItem *item;
  353. if (!rootEntry) {
  354. if (mode != listMode)
  355. goto update;
  356. QTreeWidgetItemIterator it(this);
  357. while (*it) {
  358. item = (ConfigItem*)(*it);
  359. if (!item->menu)
  360. continue;
  361. item->testUpdateMenu(menu_is_visible(item->menu));
  362. ++it;
  363. }
  364. return;
  365. }
  366. if (rootEntry != &rootmenu && (mode == singleMode ||
  367. (mode == symbolMode && rootEntry->parent != &rootmenu))) {
  368. item = (ConfigItem *)topLevelItem(0);
  369. if (!item)
  370. item = new ConfigItem(this, 0, true);
  371. last = item;
  372. }
  373. if ((mode == singleMode || (mode == symbolMode && !(rootEntry->flags & MENU_ROOT))) &&
  374. rootEntry->sym && rootEntry->prompt) {
  375. item = last ? last->nextSibling() : nullptr;
  376. if (!item)
  377. item = new ConfigItem(this, last, rootEntry, true);
  378. else
  379. item->testUpdateMenu(true);
  380. updateMenuList(item, rootEntry);
  381. update();
  382. resizeColumnToContents(0);
  383. return;
  384. }
  385. update:
  386. updateMenuList(rootEntry);
  387. update();
  388. resizeColumnToContents(0);
  389. }
  390. void ConfigList::updateListForAll()
  391. {
  392. QListIterator<ConfigList *> it(allLists);
  393. while (it.hasNext()) {
  394. ConfigList *list = it.next();
  395. list->updateList();
  396. }
  397. }
  398. void ConfigList::updateListAllForAll()
  399. {
  400. QListIterator<ConfigList *> it(allLists);
  401. while (it.hasNext()) {
  402. ConfigList *list = it.next();
  403. list->updateList();
  404. }
  405. }
  406. void ConfigList::setValue(ConfigItem* item, tristate val)
  407. {
  408. struct symbol* sym;
  409. int type;
  410. tristate oldval;
  411. sym = item->menu ? item->menu->sym : 0;
  412. if (!sym)
  413. return;
  414. type = sym_get_type(sym);
  415. switch (type) {
  416. case S_BOOLEAN:
  417. case S_TRISTATE:
  418. oldval = sym_get_tristate_value(sym);
  419. if (!sym_set_tristate_value(sym, val))
  420. return;
  421. if (oldval == no && item->menu->list)
  422. item->setExpanded(true);
  423. ConfigList::updateListForAll();
  424. break;
  425. }
  426. }
  427. void ConfigList::changeValue(ConfigItem* item)
  428. {
  429. struct symbol* sym;
  430. struct menu* menu;
  431. int type, oldexpr, newexpr;
  432. menu = item->menu;
  433. if (!menu)
  434. return;
  435. sym = menu->sym;
  436. if (!sym) {
  437. if (item->menu->list)
  438. item->setExpanded(!item->isExpanded());
  439. return;
  440. }
  441. type = sym_get_type(sym);
  442. switch (type) {
  443. case S_BOOLEAN:
  444. case S_TRISTATE:
  445. oldexpr = sym_get_tristate_value(sym);
  446. newexpr = sym_toggle_tristate_value(sym);
  447. if (item->menu->list) {
  448. if (oldexpr == newexpr)
  449. item->setExpanded(!item->isExpanded());
  450. else if (oldexpr == no)
  451. item->setExpanded(true);
  452. }
  453. if (oldexpr != newexpr)
  454. ConfigList::updateListForAll();
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. void ConfigList::setRootMenu(struct menu *menu)
  461. {
  462. enum prop_type type;
  463. if (rootEntry == menu)
  464. return;
  465. type = menu && menu->prompt ? menu->prompt->type : P_UNKNOWN;
  466. if (type != P_MENU)
  467. return;
  468. updateMenuList(0);
  469. rootEntry = menu;
  470. updateListAll();
  471. if (currentItem()) {
  472. setSelected(currentItem(), hasFocus());
  473. scrollToItem(currentItem());
  474. }
  475. }
  476. void ConfigList::setParentMenu(void)
  477. {
  478. ConfigItem* item;
  479. struct menu *oldroot;
  480. oldroot = rootEntry;
  481. if (rootEntry == &rootmenu)
  482. return;
  483. setRootMenu(menu_get_parent_menu(rootEntry->parent));
  484. QTreeWidgetItemIterator it(this);
  485. while (*it) {
  486. item = (ConfigItem *)(*it);
  487. if (item->menu == oldroot) {
  488. setCurrentItem(item);
  489. scrollToItem(item);
  490. break;
  491. }
  492. ++it;
  493. }
  494. }
  495. /*
  496. * update all the children of a menu entry
  497. * removes/adds the entries from the parent widget as necessary
  498. *
  499. * parent: either the menu list widget or a menu entry widget
  500. * menu: entry to be updated
  501. */
  502. void ConfigList::updateMenuList(ConfigItem *parent, struct menu* menu)
  503. {
  504. struct menu* child;
  505. ConfigItem* item;
  506. ConfigItem* last;
  507. bool visible;
  508. enum prop_type type;
  509. if (!menu) {
  510. while (parent->childCount() > 0)
  511. {
  512. delete parent->takeChild(0);
  513. }
  514. return;
  515. }
  516. last = parent->firstChild();
  517. if (last && !last->goParent)
  518. last = 0;
  519. for (child = menu->list; child; child = child->next) {
  520. item = last ? last->nextSibling() : parent->firstChild();
  521. type = child->prompt ? child->prompt->type : P_UNKNOWN;
  522. switch (mode) {
  523. case menuMode:
  524. if (!(child->flags & MENU_ROOT))
  525. goto hide;
  526. break;
  527. case symbolMode:
  528. if (child->flags & MENU_ROOT)
  529. goto hide;
  530. break;
  531. default:
  532. break;
  533. }
  534. visible = menu_is_visible(child);
  535. if (!menuSkip(child)) {
  536. if (!child->sym && !child->list && !child->prompt)
  537. continue;
  538. if (!item || item->menu != child)
  539. item = new ConfigItem(parent, last, child, visible);
  540. else
  541. item->testUpdateMenu(visible);
  542. if (mode == fullMode || mode == menuMode || type != P_MENU)
  543. updateMenuList(item, child);
  544. else
  545. updateMenuList(item, 0);
  546. last = item;
  547. continue;
  548. }
  549. hide:
  550. if (item && item->menu == child) {
  551. last = parent->firstChild();
  552. if (last == item)
  553. last = 0;
  554. else while (last->nextSibling() != item)
  555. last = last->nextSibling();
  556. delete item;
  557. }
  558. }
  559. }
  560. void ConfigList::updateMenuList(struct menu *menu)
  561. {
  562. struct menu* child;
  563. ConfigItem* item;
  564. ConfigItem* last;
  565. bool visible;
  566. enum prop_type type;
  567. if (!menu) {
  568. while (topLevelItemCount() > 0)
  569. {
  570. delete takeTopLevelItem(0);
  571. }
  572. return;
  573. }
  574. last = (ConfigItem *)topLevelItem(0);
  575. if (last && !last->goParent)
  576. last = 0;
  577. for (child = menu->list; child; child = child->next) {
  578. item = last ? last->nextSibling() : (ConfigItem *)topLevelItem(0);
  579. type = child->prompt ? child->prompt->type : P_UNKNOWN;
  580. switch (mode) {
  581. case menuMode:
  582. if (!(child->flags & MENU_ROOT))
  583. goto hide;
  584. break;
  585. case symbolMode:
  586. if (child->flags & MENU_ROOT)
  587. goto hide;
  588. break;
  589. default:
  590. break;
  591. }
  592. visible = menu_is_visible(child);
  593. if (!menuSkip(child)) {
  594. if (!child->sym && !child->list && !child->prompt)
  595. continue;
  596. if (!item || item->menu != child)
  597. item = new ConfigItem(this, last, child, visible);
  598. else
  599. item->testUpdateMenu(visible);
  600. if (mode == fullMode || mode == menuMode || type != P_MENU)
  601. updateMenuList(item, child);
  602. else
  603. updateMenuList(item, 0);
  604. last = item;
  605. continue;
  606. }
  607. hide:
  608. if (item && item->menu == child) {
  609. last = (ConfigItem *)topLevelItem(0);
  610. if (last == item)
  611. last = 0;
  612. else while (last->nextSibling() != item)
  613. last = last->nextSibling();
  614. delete item;
  615. }
  616. }
  617. }
  618. void ConfigList::keyPressEvent(QKeyEvent* ev)
  619. {
  620. QTreeWidgetItem* i = currentItem();
  621. ConfigItem* item;
  622. struct menu *menu;
  623. enum prop_type type;
  624. if (ev->key() == Qt::Key_Escape && mode != fullMode && mode != listMode) {
  625. emit parentSelected();
  626. ev->accept();
  627. return;
  628. }
  629. if (!i) {
  630. Parent::keyPressEvent(ev);
  631. return;
  632. }
  633. item = (ConfigItem*)i;
  634. switch (ev->key()) {
  635. case Qt::Key_Return:
  636. case Qt::Key_Enter:
  637. if (item->goParent) {
  638. emit parentSelected();
  639. break;
  640. }
  641. menu = item->menu;
  642. if (!menu)
  643. break;
  644. type = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  645. if (type == P_MENU && rootEntry != menu &&
  646. mode != fullMode && mode != menuMode) {
  647. if (mode == menuMode)
  648. emit menuSelected(menu);
  649. else
  650. emit itemSelected(menu);
  651. break;
  652. }
  653. case Qt::Key_Space:
  654. changeValue(item);
  655. break;
  656. case Qt::Key_N:
  657. setValue(item, no);
  658. break;
  659. case Qt::Key_M:
  660. setValue(item, mod);
  661. break;
  662. case Qt::Key_Y:
  663. setValue(item, yes);
  664. break;
  665. default:
  666. Parent::keyPressEvent(ev);
  667. return;
  668. }
  669. ev->accept();
  670. }
  671. void ConfigList::mousePressEvent(QMouseEvent* e)
  672. {
  673. //QPoint p(contentsToViewport(e->pos()));
  674. //printf("contentsMousePressEvent: %d,%d\n", p.x(), p.y());
  675. Parent::mousePressEvent(e);
  676. }
  677. void ConfigList::mouseReleaseEvent(QMouseEvent* e)
  678. {
  679. QPoint p = e->pos();
  680. ConfigItem* item = (ConfigItem*)itemAt(p);
  681. struct menu *menu;
  682. enum prop_type ptype;
  683. QIcon icon;
  684. int idx, x;
  685. if (!item)
  686. goto skip;
  687. menu = item->menu;
  688. x = header()->offset() + p.x();
  689. idx = header()->logicalIndexAt(x);
  690. switch (idx) {
  691. case promptColIdx:
  692. icon = item->icon(promptColIdx);
  693. if (!icon.isNull()) {
  694. int off = header()->sectionPosition(0) + visualRect(indexAt(p)).x() + 4; // 4 is Hardcoded image offset. There might be a way to do it properly.
  695. if (x >= off && x < off + icon.availableSizes().first().width()) {
  696. if (item->goParent) {
  697. emit parentSelected();
  698. break;
  699. } else if (!menu)
  700. break;
  701. ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  702. if (ptype == P_MENU && rootEntry != menu &&
  703. mode != fullMode && mode != menuMode &&
  704. mode != listMode)
  705. emit menuSelected(menu);
  706. else
  707. changeValue(item);
  708. }
  709. }
  710. break;
  711. case dataColIdx:
  712. changeValue(item);
  713. break;
  714. }
  715. skip:
  716. //printf("contentsMouseReleaseEvent: %d,%d\n", p.x(), p.y());
  717. Parent::mouseReleaseEvent(e);
  718. }
  719. void ConfigList::mouseMoveEvent(QMouseEvent* e)
  720. {
  721. //QPoint p(contentsToViewport(e->pos()));
  722. //printf("contentsMouseMoveEvent: %d,%d\n", p.x(), p.y());
  723. Parent::mouseMoveEvent(e);
  724. }
  725. void ConfigList::mouseDoubleClickEvent(QMouseEvent* e)
  726. {
  727. QPoint p = e->pos();
  728. ConfigItem* item = (ConfigItem*)itemAt(p);
  729. struct menu *menu;
  730. enum prop_type ptype;
  731. if (!item)
  732. goto skip;
  733. if (item->goParent) {
  734. emit parentSelected();
  735. goto skip;
  736. }
  737. menu = item->menu;
  738. if (!menu)
  739. goto skip;
  740. ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  741. if (ptype == P_MENU && mode != listMode) {
  742. if (mode == singleMode)
  743. emit itemSelected(menu);
  744. else if (mode == symbolMode)
  745. emit menuSelected(menu);
  746. } else if (menu->sym)
  747. changeValue(item);
  748. skip:
  749. //printf("contentsMouseDoubleClickEvent: %d,%d\n", p.x(), p.y());
  750. Parent::mouseDoubleClickEvent(e);
  751. }
  752. void ConfigList::focusInEvent(QFocusEvent *e)
  753. {
  754. struct menu *menu = NULL;
  755. Parent::focusInEvent(e);
  756. ConfigItem* item = (ConfigItem *)currentItem();
  757. if (item) {
  758. setSelected(item, true);
  759. menu = item->menu;
  760. }
  761. emit gotFocus(menu);
  762. }
  763. void ConfigList::contextMenuEvent(QContextMenuEvent *e)
  764. {
  765. if (!headerPopup) {
  766. QAction *action;
  767. headerPopup = new QMenu(this);
  768. action = new QAction("Show Name", this);
  769. action->setCheckable(true);
  770. connect(action, &QAction::toggled,
  771. this, &ConfigList::setShowName);
  772. connect(this, &ConfigList::showNameChanged,
  773. action, &QAction::setChecked);
  774. action->setChecked(showName);
  775. headerPopup->addAction(action);
  776. }
  777. headerPopup->exec(e->globalPos());
  778. e->accept();
  779. }
  780. void ConfigList::setShowName(bool on)
  781. {
  782. if (showName == on)
  783. return;
  784. showName = on;
  785. reinit();
  786. emit showNameChanged(on);
  787. }
  788. QList<ConfigList *> ConfigList::allLists;
  789. QAction *ConfigList::showNormalAction;
  790. QAction *ConfigList::showAllAction;
  791. QAction *ConfigList::showPromptAction;
  792. void ConfigList::setAllOpen(bool open)
  793. {
  794. QTreeWidgetItemIterator it(this);
  795. while (*it) {
  796. (*it)->setExpanded(open);
  797. ++it;
  798. }
  799. }
  800. ConfigInfoView::ConfigInfoView(QWidget* parent, const char *name)
  801. : Parent(parent), sym(0), _menu(0)
  802. {
  803. setObjectName(name);
  804. setOpenLinks(false);
  805. if (!objectName().isEmpty()) {
  806. configSettings->beginGroup(objectName());
  807. setShowDebug(configSettings->value("/showDebug", false).toBool());
  808. configSettings->endGroup();
  809. connect(configApp, &QApplication::aboutToQuit,
  810. this, &ConfigInfoView::saveSettings);
  811. }
  812. contextMenu = createStandardContextMenu();
  813. QAction *action = new QAction("Show Debug Info", contextMenu);
  814. action->setCheckable(true);
  815. connect(action, &QAction::toggled,
  816. this, &ConfigInfoView::setShowDebug);
  817. connect(this, &ConfigInfoView::showDebugChanged,
  818. action, &QAction::setChecked);
  819. action->setChecked(showDebug());
  820. contextMenu->addSeparator();
  821. contextMenu->addAction(action);
  822. }
  823. void ConfigInfoView::saveSettings(void)
  824. {
  825. if (!objectName().isEmpty()) {
  826. configSettings->beginGroup(objectName());
  827. configSettings->setValue("/showDebug", showDebug());
  828. configSettings->endGroup();
  829. }
  830. }
  831. void ConfigInfoView::setShowDebug(bool b)
  832. {
  833. if (_showDebug != b) {
  834. _showDebug = b;
  835. if (_menu)
  836. menuInfo();
  837. else if (sym)
  838. symbolInfo();
  839. emit showDebugChanged(b);
  840. }
  841. }
  842. void ConfigInfoView::setInfo(struct menu *m)
  843. {
  844. if (_menu == m)
  845. return;
  846. _menu = m;
  847. sym = NULL;
  848. if (!_menu)
  849. clear();
  850. else
  851. menuInfo();
  852. }
  853. void ConfigInfoView::symbolInfo(void)
  854. {
  855. QString str;
  856. str += "<big>Symbol: <b>";
  857. str += print_filter(sym->name);
  858. str += "</b></big><br><br>value: ";
  859. str += print_filter(sym_get_string_value(sym));
  860. str += "<br>visibility: ";
  861. str += sym->visible == yes ? "y" : sym->visible == mod ? "m" : "n";
  862. str += "<br>";
  863. str += debug_info(sym);
  864. setText(str);
  865. }
  866. void ConfigInfoView::menuInfo(void)
  867. {
  868. struct symbol* sym;
  869. QString info;
  870. QTextStream stream(&info);
  871. sym = _menu->sym;
  872. if (sym) {
  873. if (_menu->prompt) {
  874. stream << "<big><b>";
  875. stream << print_filter(_menu->prompt->text);
  876. stream << "</b></big>";
  877. if (sym->name) {
  878. stream << " (";
  879. if (showDebug())
  880. stream << "<a href=\"s" << sym->name << "\">";
  881. stream << print_filter(sym->name);
  882. if (showDebug())
  883. stream << "</a>";
  884. stream << ")";
  885. }
  886. } else if (sym->name) {
  887. stream << "<big><b>";
  888. if (showDebug())
  889. stream << "<a href=\"s" << sym->name << "\">";
  890. stream << print_filter(sym->name);
  891. if (showDebug())
  892. stream << "</a>";
  893. stream << "</b></big>";
  894. }
  895. stream << "<br><br>";
  896. if (showDebug())
  897. stream << debug_info(sym);
  898. struct gstr help_gstr = str_new();
  899. menu_get_ext_help(_menu, &help_gstr);
  900. stream << print_filter(str_get(&help_gstr));
  901. str_free(&help_gstr);
  902. } else if (_menu->prompt) {
  903. stream << "<big><b>";
  904. stream << print_filter(_menu->prompt->text);
  905. stream << "</b></big><br><br>";
  906. if (showDebug()) {
  907. if (_menu->prompt->visible.expr) {
  908. stream << "&nbsp;&nbsp;dep: ";
  909. expr_print(_menu->prompt->visible.expr,
  910. expr_print_help, &stream, E_NONE);
  911. stream << "<br><br>";
  912. }
  913. stream << "defined at " << _menu->file->name << ":"
  914. << _menu->lineno << "<br><br>";
  915. }
  916. }
  917. setText(info);
  918. }
  919. QString ConfigInfoView::debug_info(struct symbol *sym)
  920. {
  921. QString debug;
  922. QTextStream stream(&debug);
  923. stream << "type: ";
  924. stream << print_filter(sym_type_name(sym->type));
  925. if (sym_is_choice(sym))
  926. stream << " (choice)";
  927. debug += "<br>";
  928. if (sym->rev_dep.expr) {
  929. stream << "reverse dep: ";
  930. expr_print(sym->rev_dep.expr, expr_print_help, &stream, E_NONE);
  931. stream << "<br>";
  932. }
  933. for (struct property *prop = sym->prop; prop; prop = prop->next) {
  934. switch (prop->type) {
  935. case P_PROMPT:
  936. case P_MENU:
  937. stream << "prompt: <a href=\"m" << sym->name << "\">";
  938. stream << print_filter(prop->text);
  939. stream << "</a><br>";
  940. break;
  941. case P_DEFAULT:
  942. case P_SELECT:
  943. case P_RANGE:
  944. case P_COMMENT:
  945. case P_IMPLY:
  946. case P_SYMBOL:
  947. stream << prop_get_type_name(prop->type);
  948. stream << ": ";
  949. expr_print(prop->expr, expr_print_help,
  950. &stream, E_NONE);
  951. stream << "<br>";
  952. break;
  953. case P_CHOICE:
  954. if (sym_is_choice(sym)) {
  955. stream << "choice: ";
  956. expr_print(prop->expr, expr_print_help,
  957. &stream, E_NONE);
  958. stream << "<br>";
  959. }
  960. break;
  961. default:
  962. stream << "unknown property: ";
  963. stream << prop_get_type_name(prop->type);
  964. stream << "<br>";
  965. }
  966. if (prop->visible.expr) {
  967. stream << "&nbsp;&nbsp;&nbsp;&nbsp;dep: ";
  968. expr_print(prop->visible.expr, expr_print_help,
  969. &stream, E_NONE);
  970. stream << "<br>";
  971. }
  972. }
  973. stream << "<br>";
  974. return debug;
  975. }
  976. QString ConfigInfoView::print_filter(const QString &str)
  977. {
  978. QRegularExpression re("[<>&\"\\n]");
  979. QString res = str;
  980. for (int i = 0; (i = res.indexOf(re, i)) >= 0;) {
  981. switch (res[i].toLatin1()) {
  982. case '<':
  983. res.replace(i, 1, "&lt;");
  984. i += 4;
  985. break;
  986. case '>':
  987. res.replace(i, 1, "&gt;");
  988. i += 4;
  989. break;
  990. case '&':
  991. res.replace(i, 1, "&amp;");
  992. i += 5;
  993. break;
  994. case '"':
  995. res.replace(i, 1, "&quot;");
  996. i += 6;
  997. break;
  998. case '\n':
  999. res.replace(i, 1, "<br>");
  1000. i += 4;
  1001. break;
  1002. }
  1003. }
  1004. return res;
  1005. }
  1006. void ConfigInfoView::expr_print_help(void *data, struct symbol *sym, const char *str)
  1007. {
  1008. QTextStream *stream = reinterpret_cast<QTextStream *>(data);
  1009. if (sym && sym->name && !(sym->flags & SYMBOL_CONST)) {
  1010. *stream << "<a href=\"s" << sym->name << "\">";
  1011. *stream << print_filter(str);
  1012. *stream << "</a>";
  1013. } else {
  1014. *stream << print_filter(str);
  1015. }
  1016. }
  1017. void ConfigInfoView::clicked(const QUrl &url)
  1018. {
  1019. QByteArray str = url.toEncoded();
  1020. const std::size_t count = str.size();
  1021. char *data = new char[count + 1];
  1022. struct symbol **result;
  1023. struct menu *m = NULL;
  1024. if (count < 1) {
  1025. delete[] data;
  1026. return;
  1027. }
  1028. memcpy(data, str.constData(), count);
  1029. data[count] = '\0';
  1030. /* Seek for exact match */
  1031. data[0] = '^';
  1032. strcat(data, "$");
  1033. result = sym_re_search(data);
  1034. if (!result) {
  1035. delete[] data;
  1036. return;
  1037. }
  1038. sym = *result;
  1039. /* Seek for the menu which holds the symbol */
  1040. for (struct property *prop = sym->prop; prop; prop = prop->next) {
  1041. if (prop->type != P_PROMPT && prop->type != P_MENU)
  1042. continue;
  1043. m = prop->menu;
  1044. break;
  1045. }
  1046. if (!m) {
  1047. /* Symbol is not visible as a menu */
  1048. symbolInfo();
  1049. emit showDebugChanged(true);
  1050. } else {
  1051. emit menuSelected(m);
  1052. }
  1053. free(result);
  1054. delete[] data;
  1055. }
  1056. void ConfigInfoView::contextMenuEvent(QContextMenuEvent *event)
  1057. {
  1058. contextMenu->popup(event->globalPos());
  1059. event->accept();
  1060. }
  1061. ConfigSearchWindow::ConfigSearchWindow(ConfigMainWindow *parent)
  1062. : Parent(parent), result(NULL)
  1063. {
  1064. setObjectName("search");
  1065. setWindowTitle("Search Config");
  1066. QVBoxLayout* layout1 = new QVBoxLayout(this);
  1067. layout1->setContentsMargins(11, 11, 11, 11);
  1068. layout1->setSpacing(6);
  1069. QHBoxLayout* layout2 = new QHBoxLayout();
  1070. layout2->setContentsMargins(0, 0, 0, 0);
  1071. layout2->setSpacing(6);
  1072. layout2->addWidget(new QLabel("Find:", this));
  1073. editField = new QLineEdit(this);
  1074. connect(editField, &QLineEdit::returnPressed,
  1075. this, &ConfigSearchWindow::search);
  1076. layout2->addWidget(editField);
  1077. searchButton = new QPushButton("Search", this);
  1078. searchButton->setAutoDefault(false);
  1079. connect(searchButton, &QPushButton::clicked,
  1080. this, &ConfigSearchWindow::search);
  1081. layout2->addWidget(searchButton);
  1082. layout1->addLayout(layout2);
  1083. split = new QSplitter(this);
  1084. split->setOrientation(Qt::Vertical);
  1085. list = new ConfigList(split, "search");
  1086. list->mode = listMode;
  1087. info = new ConfigInfoView(split, "search");
  1088. connect(list, &ConfigList::menuChanged,
  1089. info, &ConfigInfoView::setInfo);
  1090. connect(list, &ConfigList::menuChanged,
  1091. parent, &ConfigMainWindow::setMenuLink);
  1092. layout1->addWidget(split);
  1093. QVariant x, y;
  1094. int width, height;
  1095. bool ok;
  1096. configSettings->beginGroup("search");
  1097. width = configSettings->value("/window width", parent->width() / 2).toInt();
  1098. height = configSettings->value("/window height", parent->height() / 2).toInt();
  1099. resize(width, height);
  1100. x = configSettings->value("/window x");
  1101. y = configSettings->value("/window y");
  1102. if (x.isValid() && y.isValid())
  1103. move(x.toInt(), y.toInt());
  1104. QList<int> sizes = configSettings->readSizes("/split", &ok);
  1105. if (ok)
  1106. split->setSizes(sizes);
  1107. configSettings->endGroup();
  1108. connect(configApp, &QApplication::aboutToQuit,
  1109. this, &ConfigSearchWindow::saveSettings);
  1110. }
  1111. void ConfigSearchWindow::saveSettings(void)
  1112. {
  1113. if (!objectName().isEmpty()) {
  1114. configSettings->beginGroup(objectName());
  1115. configSettings->setValue("/window x", pos().x());
  1116. configSettings->setValue("/window y", pos().y());
  1117. configSettings->setValue("/window width", size().width());
  1118. configSettings->setValue("/window height", size().height());
  1119. configSettings->writeSizes("/split", split->sizes());
  1120. configSettings->endGroup();
  1121. }
  1122. }
  1123. void ConfigSearchWindow::search(void)
  1124. {
  1125. struct symbol **p;
  1126. struct property *prop;
  1127. ConfigItem *lastItem = NULL;
  1128. free(result);
  1129. list->clear();
  1130. info->clear();
  1131. result = sym_re_search(editField->text().toLatin1());
  1132. if (!result)
  1133. return;
  1134. for (p = result; *p; p++) {
  1135. for_all_prompts((*p), prop)
  1136. lastItem = new ConfigItem(list, lastItem, prop->menu,
  1137. menu_is_visible(prop->menu));
  1138. }
  1139. }
  1140. /*
  1141. * Construct the complete config widget
  1142. */
  1143. ConfigMainWindow::ConfigMainWindow(void)
  1144. : searchWindow(0)
  1145. {
  1146. bool ok = true;
  1147. QVariant x, y;
  1148. int width, height;
  1149. char title[256];
  1150. snprintf(title, sizeof(title), "%s%s",
  1151. rootmenu.prompt->text,
  1152. ""
  1153. );
  1154. setWindowTitle(title);
  1155. QRect g = configApp->primaryScreen()->geometry();
  1156. width = configSettings->value("/window width", g.width() - 64).toInt();
  1157. height = configSettings->value("/window height", g.height() - 64).toInt();
  1158. resize(width, height);
  1159. x = configSettings->value("/window x");
  1160. y = configSettings->value("/window y");
  1161. if ((x.isValid())&&(y.isValid()))
  1162. move(x.toInt(), y.toInt());
  1163. // set up icons
  1164. ConfigItem::symbolYesIcon = QIcon(QPixmap(xpm_symbol_yes));
  1165. ConfigItem::symbolModIcon = QIcon(QPixmap(xpm_symbol_mod));
  1166. ConfigItem::symbolNoIcon = QIcon(QPixmap(xpm_symbol_no));
  1167. ConfigItem::choiceYesIcon = QIcon(QPixmap(xpm_choice_yes));
  1168. ConfigItem::choiceNoIcon = QIcon(QPixmap(xpm_choice_no));
  1169. ConfigItem::menuIcon = QIcon(QPixmap(xpm_menu));
  1170. ConfigItem::menubackIcon = QIcon(QPixmap(xpm_menuback));
  1171. QWidget *widget = new QWidget(this);
  1172. QVBoxLayout *layout = new QVBoxLayout(widget);
  1173. setCentralWidget(widget);
  1174. split1 = new QSplitter(widget);
  1175. split1->setOrientation(Qt::Horizontal);
  1176. split1->setChildrenCollapsible(false);
  1177. menuList = new ConfigList(widget, "menu");
  1178. split2 = new QSplitter(widget);
  1179. split2->setChildrenCollapsible(false);
  1180. split2->setOrientation(Qt::Vertical);
  1181. // create config tree
  1182. configList = new ConfigList(widget, "config");
  1183. helpText = new ConfigInfoView(widget, "help");
  1184. layout->addWidget(split2);
  1185. split2->addWidget(split1);
  1186. split1->addWidget(configList);
  1187. split1->addWidget(menuList);
  1188. split2->addWidget(helpText);
  1189. setTabOrder(configList, helpText);
  1190. configList->setFocus();
  1191. backAction = new QAction(QPixmap(xpm_back), "Back", this);
  1192. connect(backAction, &QAction::triggered,
  1193. this, &ConfigMainWindow::goBack);
  1194. QAction *quitAction = new QAction("&Quit", this);
  1195. quitAction->setShortcut(Qt::CTRL | Qt::Key_Q);
  1196. connect(quitAction, &QAction::triggered,
  1197. this, &ConfigMainWindow::close);
  1198. QAction *loadAction = new QAction(QPixmap(xpm_load), "&Load", this);
  1199. loadAction->setShortcut(Qt::CTRL | Qt::Key_L);
  1200. connect(loadAction, &QAction::triggered,
  1201. this, &ConfigMainWindow::loadConfig);
  1202. saveAction = new QAction(QPixmap(xpm_save), "&Save", this);
  1203. saveAction->setShortcut(Qt::CTRL | Qt::Key_S);
  1204. connect(saveAction, &QAction::triggered,
  1205. this, &ConfigMainWindow::saveConfig);
  1206. conf_set_changed_callback(conf_changed);
  1207. // Set saveAction's initial state
  1208. conf_changed();
  1209. configname = xstrdup(conf_get_configname());
  1210. QAction *saveAsAction = new QAction("Save &As...", this);
  1211. connect(saveAsAction, &QAction::triggered,
  1212. this, &ConfigMainWindow::saveConfigAs);
  1213. QAction *searchAction = new QAction("&Find", this);
  1214. searchAction->setShortcut(Qt::CTRL | Qt::Key_F);
  1215. connect(searchAction, &QAction::triggered,
  1216. this, &ConfigMainWindow::searchConfig);
  1217. singleViewAction = new QAction(QPixmap(xpm_single_view), "Single View", this);
  1218. singleViewAction->setCheckable(true);
  1219. connect(singleViewAction, &QAction::triggered,
  1220. this, &ConfigMainWindow::showSingleView);
  1221. splitViewAction = new QAction(QPixmap(xpm_split_view), "Split View", this);
  1222. splitViewAction->setCheckable(true);
  1223. connect(splitViewAction, &QAction::triggered,
  1224. this, &ConfigMainWindow::showSplitView);
  1225. fullViewAction = new QAction(QPixmap(xpm_tree_view), "Full View", this);
  1226. fullViewAction->setCheckable(true);
  1227. connect(fullViewAction, &QAction::triggered,
  1228. this, &ConfigMainWindow::showFullView);
  1229. QAction *showNameAction = new QAction("Show Name", this);
  1230. showNameAction->setCheckable(true);
  1231. connect(showNameAction, &QAction::toggled,
  1232. configList, &ConfigList::setShowName);
  1233. showNameAction->setChecked(configList->showName);
  1234. QActionGroup *optGroup = new QActionGroup(this);
  1235. optGroup->setExclusive(true);
  1236. connect(optGroup, &QActionGroup::triggered,
  1237. configList, &ConfigList::setOptionMode);
  1238. connect(optGroup, &QActionGroup::triggered,
  1239. menuList, &ConfigList::setOptionMode);
  1240. ConfigList::showNormalAction = new QAction("Show Normal Options", optGroup);
  1241. ConfigList::showNormalAction->setCheckable(true);
  1242. ConfigList::showAllAction = new QAction("Show All Options", optGroup);
  1243. ConfigList::showAllAction->setCheckable(true);
  1244. ConfigList::showPromptAction = new QAction("Show Prompt Options", optGroup);
  1245. ConfigList::showPromptAction->setCheckable(true);
  1246. QAction *showDebugAction = new QAction("Show Debug Info", this);
  1247. showDebugAction->setCheckable(true);
  1248. connect(showDebugAction, &QAction::toggled,
  1249. helpText, &ConfigInfoView::setShowDebug);
  1250. showDebugAction->setChecked(helpText->showDebug());
  1251. QAction *showIntroAction = new QAction("Introduction", this);
  1252. connect(showIntroAction, &QAction::triggered,
  1253. this, &ConfigMainWindow::showIntro);
  1254. QAction *showAboutAction = new QAction("About", this);
  1255. connect(showAboutAction, &QAction::triggered,
  1256. this, &ConfigMainWindow::showAbout);
  1257. // init tool bar
  1258. QToolBar *toolBar = addToolBar("Tools");
  1259. toolBar->addAction(backAction);
  1260. toolBar->addSeparator();
  1261. toolBar->addAction(loadAction);
  1262. toolBar->addAction(saveAction);
  1263. toolBar->addSeparator();
  1264. toolBar->addAction(singleViewAction);
  1265. toolBar->addAction(splitViewAction);
  1266. toolBar->addAction(fullViewAction);
  1267. // create file menu
  1268. QMenu *menu = menuBar()->addMenu("&File");
  1269. menu->addAction(loadAction);
  1270. menu->addAction(saveAction);
  1271. menu->addAction(saveAsAction);
  1272. menu->addSeparator();
  1273. menu->addAction(quitAction);
  1274. // create edit menu
  1275. menu = menuBar()->addMenu("&Edit");
  1276. menu->addAction(searchAction);
  1277. // create options menu
  1278. menu = menuBar()->addMenu("&Option");
  1279. menu->addAction(showNameAction);
  1280. menu->addSeparator();
  1281. menu->addActions(optGroup->actions());
  1282. menu->addSeparator();
  1283. menu->addAction(showDebugAction);
  1284. // create help menu
  1285. menu = menuBar()->addMenu("&Help");
  1286. menu->addAction(showIntroAction);
  1287. menu->addAction(showAboutAction);
  1288. connect(helpText, &ConfigInfoView::anchorClicked,
  1289. helpText, &ConfigInfoView::clicked);
  1290. connect(configList, &ConfigList::menuChanged,
  1291. helpText, &ConfigInfoView::setInfo);
  1292. connect(configList, &ConfigList::menuSelected,
  1293. this, &ConfigMainWindow::changeMenu);
  1294. connect(configList, &ConfigList::itemSelected,
  1295. this, &ConfigMainWindow::changeItens);
  1296. connect(configList, &ConfigList::parentSelected,
  1297. this, &ConfigMainWindow::goBack);
  1298. connect(menuList, &ConfigList::menuChanged,
  1299. helpText, &ConfigInfoView::setInfo);
  1300. connect(menuList, &ConfigList::menuSelected,
  1301. this, &ConfigMainWindow::changeMenu);
  1302. connect(configList, &ConfigList::gotFocus,
  1303. helpText, &ConfigInfoView::setInfo);
  1304. connect(menuList, &ConfigList::gotFocus,
  1305. helpText, &ConfigInfoView::setInfo);
  1306. connect(menuList, &ConfigList::gotFocus,
  1307. this, &ConfigMainWindow::listFocusChanged);
  1308. connect(helpText, &ConfigInfoView::menuSelected,
  1309. this, &ConfigMainWindow::setMenuLink);
  1310. QString listMode = configSettings->value("/listMode", "symbol").toString();
  1311. if (listMode == "single")
  1312. showSingleView();
  1313. else if (listMode == "full")
  1314. showFullView();
  1315. else /*if (listMode == "split")*/
  1316. showSplitView();
  1317. // UI setup done, restore splitter positions
  1318. QList<int> sizes = configSettings->readSizes("/split1", &ok);
  1319. if (ok)
  1320. split1->setSizes(sizes);
  1321. sizes = configSettings->readSizes("/split2", &ok);
  1322. if (ok)
  1323. split2->setSizes(sizes);
  1324. }
  1325. void ConfigMainWindow::loadConfig(void)
  1326. {
  1327. QString str;
  1328. QByteArray ba;
  1329. const char *name;
  1330. str = QFileDialog::getOpenFileName(this, "", configname);
  1331. if (str.isNull())
  1332. return;
  1333. ba = str.toLocal8Bit();
  1334. name = ba.data();
  1335. if (conf_read(name))
  1336. QMessageBox::information(this, "qconf", "Unable to load configuration!");
  1337. free(configname);
  1338. configname = xstrdup(name);
  1339. ConfigList::updateListAllForAll();
  1340. }
  1341. bool ConfigMainWindow::saveConfig(void)
  1342. {
  1343. if (conf_write(configname)) {
  1344. QMessageBox::information(this, "qconf", "Unable to save configuration!");
  1345. return false;
  1346. }
  1347. conf_write_autoconf(0);
  1348. return true;
  1349. }
  1350. void ConfigMainWindow::saveConfigAs(void)
  1351. {
  1352. QString str;
  1353. QByteArray ba;
  1354. const char *name;
  1355. str = QFileDialog::getSaveFileName(this, "", configname);
  1356. if (str.isNull())
  1357. return;
  1358. ba = str.toLocal8Bit();
  1359. name = ba.data();
  1360. if (conf_write(name)) {
  1361. QMessageBox::information(this, "qconf", "Unable to save configuration!");
  1362. }
  1363. conf_write_autoconf(0);
  1364. free(configname);
  1365. configname = xstrdup(name);
  1366. }
  1367. void ConfigMainWindow::searchConfig(void)
  1368. {
  1369. if (!searchWindow)
  1370. searchWindow = new ConfigSearchWindow(this);
  1371. searchWindow->show();
  1372. }
  1373. void ConfigMainWindow::changeItens(struct menu *menu)
  1374. {
  1375. configList->setRootMenu(menu);
  1376. }
  1377. void ConfigMainWindow::changeMenu(struct menu *menu)
  1378. {
  1379. menuList->setRootMenu(menu);
  1380. }
  1381. void ConfigMainWindow::setMenuLink(struct menu *menu)
  1382. {
  1383. struct menu *parent;
  1384. ConfigList* list = NULL;
  1385. ConfigItem* item;
  1386. if (configList->menuSkip(menu))
  1387. return;
  1388. switch (configList->mode) {
  1389. case singleMode:
  1390. list = configList;
  1391. parent = menu_get_parent_menu(menu);
  1392. if (!parent)
  1393. return;
  1394. list->setRootMenu(parent);
  1395. break;
  1396. case menuMode:
  1397. if (menu->flags & MENU_ROOT) {
  1398. menuList->setRootMenu(menu);
  1399. configList->clearSelection();
  1400. list = configList;
  1401. } else {
  1402. parent = menu_get_parent_menu(menu->parent);
  1403. if (!parent)
  1404. return;
  1405. /* Select the config view */
  1406. item = configList->findConfigItem(parent);
  1407. if (item) {
  1408. configList->setSelected(item, true);
  1409. configList->scrollToItem(item);
  1410. }
  1411. menuList->setRootMenu(parent);
  1412. menuList->clearSelection();
  1413. list = menuList;
  1414. }
  1415. break;
  1416. case fullMode:
  1417. list = configList;
  1418. break;
  1419. default:
  1420. break;
  1421. }
  1422. if (list) {
  1423. item = list->findConfigItem(menu);
  1424. if (item) {
  1425. list->setSelected(item, true);
  1426. list->scrollToItem(item);
  1427. list->setFocus();
  1428. helpText->setInfo(menu);
  1429. }
  1430. }
  1431. }
  1432. void ConfigMainWindow::listFocusChanged(void)
  1433. {
  1434. if (menuList->mode == menuMode)
  1435. configList->clearSelection();
  1436. }
  1437. void ConfigMainWindow::goBack(void)
  1438. {
  1439. if (configList->rootEntry == &rootmenu)
  1440. return;
  1441. configList->setParentMenu();
  1442. }
  1443. void ConfigMainWindow::showSingleView(void)
  1444. {
  1445. singleViewAction->setEnabled(false);
  1446. singleViewAction->setChecked(true);
  1447. splitViewAction->setEnabled(true);
  1448. splitViewAction->setChecked(false);
  1449. fullViewAction->setEnabled(true);
  1450. fullViewAction->setChecked(false);
  1451. backAction->setEnabled(true);
  1452. menuList->hide();
  1453. menuList->setRootMenu(0);
  1454. configList->mode = singleMode;
  1455. if (configList->rootEntry == &rootmenu)
  1456. configList->updateListAll();
  1457. else
  1458. configList->setRootMenu(&rootmenu);
  1459. configList->setFocus();
  1460. }
  1461. void ConfigMainWindow::showSplitView(void)
  1462. {
  1463. singleViewAction->setEnabled(true);
  1464. singleViewAction->setChecked(false);
  1465. splitViewAction->setEnabled(false);
  1466. splitViewAction->setChecked(true);
  1467. fullViewAction->setEnabled(true);
  1468. fullViewAction->setChecked(false);
  1469. backAction->setEnabled(false);
  1470. configList->mode = menuMode;
  1471. if (configList->rootEntry == &rootmenu)
  1472. configList->updateListAll();
  1473. else
  1474. configList->setRootMenu(&rootmenu);
  1475. configList->setAllOpen(true);
  1476. configApp->processEvents();
  1477. menuList->mode = symbolMode;
  1478. menuList->setRootMenu(&rootmenu);
  1479. menuList->setAllOpen(true);
  1480. menuList->show();
  1481. menuList->setFocus();
  1482. }
  1483. void ConfigMainWindow::showFullView(void)
  1484. {
  1485. singleViewAction->setEnabled(true);
  1486. singleViewAction->setChecked(false);
  1487. splitViewAction->setEnabled(true);
  1488. splitViewAction->setChecked(false);
  1489. fullViewAction->setEnabled(false);
  1490. fullViewAction->setChecked(true);
  1491. backAction->setEnabled(false);
  1492. menuList->hide();
  1493. menuList->setRootMenu(0);
  1494. configList->mode = fullMode;
  1495. if (configList->rootEntry == &rootmenu)
  1496. configList->updateListAll();
  1497. else
  1498. configList->setRootMenu(&rootmenu);
  1499. configList->setFocus();
  1500. }
  1501. /*
  1502. * ask for saving configuration before quitting
  1503. */
  1504. void ConfigMainWindow::closeEvent(QCloseEvent* e)
  1505. {
  1506. if (!conf_get_changed()) {
  1507. e->accept();
  1508. return;
  1509. }
  1510. QMessageBox mb(QMessageBox::Icon::Warning, "qconf",
  1511. "Save configuration?");
  1512. QPushButton *yb = mb.addButton(QMessageBox::Yes);
  1513. QPushButton *db = mb.addButton(QMessageBox::No);
  1514. QPushButton *cb = mb.addButton(QMessageBox::Cancel);
  1515. yb->setText("&Save Changes");
  1516. db->setText("&Discard Changes");
  1517. cb->setText("Cancel Exit");
  1518. mb.setDefaultButton(yb);
  1519. mb.setEscapeButton(cb);
  1520. switch (mb.exec()) {
  1521. case QMessageBox::Yes:
  1522. if (saveConfig())
  1523. e->accept();
  1524. else
  1525. e->ignore();
  1526. break;
  1527. case QMessageBox::No:
  1528. e->accept();
  1529. break;
  1530. case QMessageBox::Cancel:
  1531. e->ignore();
  1532. break;
  1533. }
  1534. }
  1535. void ConfigMainWindow::showIntro(void)
  1536. {
  1537. static const QString str =
  1538. "Welcome to the qconf graphical configuration tool.\n"
  1539. "\n"
  1540. "For bool and tristate options, a blank box indicates the "
  1541. "feature is disabled, a check indicates it is enabled, and a "
  1542. "dot indicates that it is to be compiled as a module. Clicking "
  1543. "on the box will cycle through the three states. For int, hex, "
  1544. "and string options, double-clicking or pressing F2 on the "
  1545. "Value cell will allow you to edit the value.\n"
  1546. "\n"
  1547. "If you do not see an option (e.g., a device driver) that you "
  1548. "believe should be present, try turning on Show All Options "
  1549. "under the Options menu. Enabling Show Debug Info will help you"
  1550. "figure out what other options must be enabled to support the "
  1551. "option you are interested in, and hyperlinks will navigate to "
  1552. "them.\n"
  1553. "\n"
  1554. "Toggling Show Debug Info under the Options menu will show the "
  1555. "dependencies, which you can then match by examining other "
  1556. "options.\n";
  1557. QMessageBox::information(this, "qconf", str);
  1558. }
  1559. void ConfigMainWindow::showAbout(void)
  1560. {
  1561. static const QString str = "qconf is Copyright (C) 2002 Roman Zippel <[email protected]>.\n"
  1562. "Copyright (C) 2015 Boris Barbulovski <[email protected]>.\n"
  1563. "\n"
  1564. "Bug reports and feature request can also be entered at http://bugzilla.kernel.org/\n"
  1565. "\n"
  1566. "Qt Version: ";
  1567. QMessageBox::information(this, "qconf", str + qVersion());
  1568. }
  1569. void ConfigMainWindow::saveSettings(void)
  1570. {
  1571. configSettings->setValue("/window x", pos().x());
  1572. configSettings->setValue("/window y", pos().y());
  1573. configSettings->setValue("/window width", size().width());
  1574. configSettings->setValue("/window height", size().height());
  1575. QString entry;
  1576. switch(configList->mode) {
  1577. case singleMode :
  1578. entry = "single";
  1579. break;
  1580. case symbolMode :
  1581. entry = "split";
  1582. break;
  1583. case fullMode :
  1584. entry = "full";
  1585. break;
  1586. default:
  1587. break;
  1588. }
  1589. configSettings->setValue("/listMode", entry);
  1590. configSettings->writeSizes("/split1", split1->sizes());
  1591. configSettings->writeSizes("/split2", split2->sizes());
  1592. }
  1593. void ConfigMainWindow::conf_changed(void)
  1594. {
  1595. if (saveAction)
  1596. saveAction->setEnabled(conf_get_changed());
  1597. }
  1598. void fixup_rootmenu(struct menu *menu)
  1599. {
  1600. struct menu *child;
  1601. static int menu_cnt = 0;
  1602. menu->flags |= MENU_ROOT;
  1603. for (child = menu->list; child; child = child->next) {
  1604. if (child->prompt && child->prompt->type == P_MENU) {
  1605. menu_cnt++;
  1606. fixup_rootmenu(child);
  1607. menu_cnt--;
  1608. } else if (!menu_cnt)
  1609. fixup_rootmenu(child);
  1610. }
  1611. }
  1612. static const char *progname;
  1613. static void usage(void)
  1614. {
  1615. printf("%s [-s] <config>\n", progname);
  1616. exit(0);
  1617. }
  1618. int main(int ac, char** av)
  1619. {
  1620. ConfigMainWindow* v;
  1621. const char *name;
  1622. progname = av[0];
  1623. if (ac > 1 && av[1][0] == '-') {
  1624. switch (av[1][1]) {
  1625. case 's':
  1626. conf_set_message_callback(NULL);
  1627. break;
  1628. case 'h':
  1629. case '?':
  1630. usage();
  1631. }
  1632. name = av[2];
  1633. } else
  1634. name = av[1];
  1635. if (!name)
  1636. usage();
  1637. conf_parse(name);
  1638. fixup_rootmenu(&rootmenu);
  1639. conf_read(NULL);
  1640. //zconfdump(stdout);
  1641. configApp = new QApplication(ac, av);
  1642. configSettings = new ConfigSettings();
  1643. configSettings->beginGroup("/kconfig/qconf");
  1644. v = new ConfigMainWindow();
  1645. //zconfdump(stdout);
  1646. configApp->connect(configApp, SIGNAL(lastWindowClosed()), SLOT(quit()));
  1647. configApp->connect(configApp, SIGNAL(aboutToQuit()), v, SLOT(saveSettings()));
  1648. v->show();
  1649. configApp->exec();
  1650. configSettings->endGroup();
  1651. delete configSettings;
  1652. delete v;
  1653. delete configApp;
  1654. return 0;
  1655. }