qconf.cc 43 KB

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