PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/init/hoveroverInit.cpp

http://github.com/dmzdev/io
C++ | 391 lines | 251 code | 140 blank | 0 comment | 42 complexity | bc591a005381275083cd61c2069b8d7e MD5 | raw file
  1. #include "hoveroverInit.h"
  2. #include <dmzApplication.h>
  3. #include <dmzAppShellExt.h>
  4. #include <dmzFoundationCommandLine.h>
  5. #include <dmzQtConfigRead.h>
  6. #include <dmzQtConfigWrite.h>
  7. #include <dmzRuntimeConfig.h>
  8. #include <dmzRuntimeConfigToTypesBase.h>
  9. #include <dmzRuntimeSession.h>
  10. #include <dmzRuntimePluginFactoryLinkSymbol.h>
  11. #include <dmzRuntimeVersion.h>
  12. #include <dmzTypesHashTableStringTemplate.h>
  13. #include <dmzFoundationXMLUtil.h>
  14. #include <QtCore/QUrl>
  15. #include <QtGui/QCloseEvent>
  16. #include <QtGui/QDesktopServices>
  17. #include <QtGui/QDesktopWidget>
  18. using namespace dmz;
  19. namespace {
  20. typedef HashTableStringTemplate<String> FileTable;
  21. typedef HashTableStringTemplate<Config> ConfigTable;
  22. static const String HoveroverName ("hoveroverInit");
  23. static const String ColorName ("color.value");
  24. static const String ResolutionName ("resolution.value");
  25. static const String ScreenName ("screen.value");
  26. static const String AAName ("aa.value");
  27. static const String PortName ("port.value");
  28. static const String GeometryName ("geometry");
  29. static void
  30. local_populate_color_table (
  31. AppShellInitStruct &init,
  32. HoveroverInit &hInit,
  33. ConfigTable &colorTable) {
  34. Config colorList;
  35. if (init.manifest.lookup_all_config ("color.type", colorList)) {
  36. ConfigIterator it;
  37. Config color;
  38. while (colorList.get_next_config (it, color)) {
  39. String value = config_to_string ("text", color);
  40. if (value) {
  41. hInit.ui.colorCombo->addItem (value.get_buffer ());
  42. Config *ptr = new Config (color);
  43. if (ptr && !colorTable.store (value, ptr)) {
  44. delete ptr; ptr = 0;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. static void
  51. local_populate_resolution_table (
  52. AppShellInitStruct &init,
  53. HoveroverInit &hInit,
  54. ConfigTable &rezTable) {
  55. Config rezList;
  56. if (init.manifest.lookup_all_config ("screen.resolution", rezList)) {
  57. ConfigIterator it;
  58. Config rez;
  59. while (rezList.get_next_config (it, rez)) {
  60. String value = config_to_string ("text", rez);
  61. if (value) {
  62. hInit.ui.resolutionCombo->addItem (value.get_buffer ());
  63. Config *ptr = new Config (rez);
  64. if (!rezTable.store (value, ptr) && ptr) {
  65. delete ptr; ptr = 0;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. static void
  72. local_restore_session (AppShellInitStruct &init, HoveroverInit &hInit) {
  73. Config session = get_session_config (HoveroverName, init.app.get_context ());
  74. const String Color = config_to_string (ColorName, session);
  75. if (Color) {
  76. const int Index = hInit.ui.colorCombo->findText (Color.get_buffer ());
  77. if (Index >= 0) { hInit.ui.colorCombo->setCurrentIndex (Index); }
  78. }
  79. const String Resolution = config_to_string (ResolutionName, session);
  80. if (Resolution) {
  81. const int Index = hInit.ui.resolutionCombo->findText (Resolution.get_buffer ());
  82. if (Index >= 0) { hInit.ui.resolutionCombo->setCurrentIndex (Index); }
  83. }
  84. const Int32 Screen = config_to_int32 (ScreenName, session, -1);
  85. if (Screen >= 0) { hInit.ui.screenBox->setValue (Screen); }
  86. const Int32 Samples = config_to_int32 (AAName, session, -1);
  87. if (Samples >= 0) { hInit.ui.aaBox->setValue (Samples); }
  88. const Int32 Port = config_to_int32 (PortName, session, -1);
  89. if (Port > 0) { hInit.ui.portBox->setValue (Port); }
  90. Config geometry;
  91. if (session.lookup_config (GeometryName, geometry)) {
  92. hInit.restoreGeometry (config_to_qbytearray (geometry));
  93. }
  94. else {
  95. QRect rect = QApplication::desktop ()->availableGeometry (&hInit);
  96. hInit.move(rect.center () - hInit.rect ().center ());
  97. }
  98. }
  99. static void
  100. local_add_config (const String &Scope, AppShellInitStruct &init) {
  101. Config configList;
  102. if (init.manifest.lookup_all_config (Scope, configList)) {
  103. ConfigIterator it;
  104. Config config;
  105. while (configList.get_next_config (it, config)) {
  106. const String Value = config_to_string ("file", config);
  107. if (Value) { init.files.append_arg (Value); }
  108. }
  109. }
  110. }
  111. static void
  112. local_setup_resolution (
  113. AppShellInitStruct &init,
  114. HoveroverInit &hInit,
  115. ConfigTable &rezTable) {
  116. Config global;
  117. init.app.get_global_config (global);
  118. Config *ptr = rezTable.lookup (qPrintable (hInit.ui.resolutionCombo->currentText ()));
  119. if (ptr) {
  120. Config attrList;
  121. ptr->lookup_all_config ("attribute", attrList);
  122. ConfigIterator it;
  123. Config attr;
  124. while (attrList.get_next_config (it, attr)) {
  125. const String Scope = config_to_string ("scope", attr);
  126. const String Value = config_to_string ("value", attr);
  127. if (Scope && Value) { global.store_attribute (Scope, Value); }
  128. }
  129. }
  130. String ScreenScope = config_to_string ("screen.scope", init.manifest);
  131. if (ScreenScope) {
  132. const String Screen = qPrintable (hInit.ui.screenBox->cleanText ());
  133. global.store_attribute (ScreenScope, Screen);
  134. }
  135. String AAScope = config_to_string ("aa.scope", init.manifest);
  136. if (AAScope) {
  137. const String Samples = qPrintable (hInit.ui.aaBox->cleanText ());
  138. global.store_attribute (AAScope, Samples);
  139. }
  140. }
  141. static void
  142. local_set_port (const Int32 Port, AppShellInitStruct &init) {
  143. Config global;
  144. init.app.get_global_config (global);
  145. String PortScope = config_to_string (
  146. "port.scope",
  147. init.manifest,
  148. "dmz.dmzNetModulePacketIOHawkNL.socket.port");
  149. if (PortScope) {
  150. global.store_attribute (PortScope, String::number (Port));
  151. }
  152. }
  153. };
  154. HoveroverInit::HoveroverInit (AppShellInitStruct &theInit) :
  155. init (theInit),
  156. _start (False) {
  157. ui.setupUi (this);
  158. }
  159. HoveroverInit::~HoveroverInit () {
  160. }
  161. void
  162. HoveroverInit::on_buttonBox_accepted () {
  163. _start = True;
  164. close ();
  165. }
  166. void
  167. HoveroverInit::on_buttonBox_rejected () {
  168. close ();
  169. }
  170. void
  171. HoveroverInit::on_buttonBox_helpRequested () {
  172. const String UrlValue =
  173. config_to_string ("help.url", init.manifest, "http://dmzdev.org/wiki/io");
  174. if (UrlValue) {
  175. QUrl Url (UrlValue.get_buffer ());
  176. QDesktopServices::openUrl (Url);
  177. }
  178. }
  179. void
  180. HoveroverInit::closeEvent (QCloseEvent * event) {
  181. if (!_start) {
  182. init.app.quit ("Cancel Button Pressed");
  183. }
  184. else {
  185. Config session (HoveroverName);
  186. const String Color = qPrintable (ui.colorCombo->currentText ());
  187. session.store_attribute (ColorName, Color);
  188. const String Resolution = qPrintable (ui.resolutionCombo->currentText ());
  189. session.store_attribute (ResolutionName, Resolution);
  190. const Int32 Screen = ui.screenBox->value ();
  191. session.store_attribute (ScreenName, String::number (Screen));
  192. const Int32 Samples = ui.aaBox->value ();
  193. session.store_attribute (AAName, String::number (Samples));
  194. const Int32 Port = ui.portBox->value ();
  195. session.store_attribute (PortName, String::number (Port));
  196. session.add_config (qbytearray_to_config ("geometry", saveGeometry ()));
  197. set_session_config (init.app.get_context (), session);
  198. }
  199. event->accept ();
  200. }
  201. extern "C" {
  202. DMZ_PLUGIN_FACTORY_LINK_SYMBOL void
  203. dmz_init_hoverover (AppShellInitStruct &init) {
  204. HoveroverInit hInit (init);
  205. if (init.VersionFile) {
  206. Version version;
  207. if (xml_to_version (init.VersionFile, version, &init.app.log)) {
  208. QString vs = hInit.windowTitle ();
  209. vs += " (v";
  210. const String Tmp = version.get_version ().get_buffer ();
  211. if (Tmp) { vs += Tmp.get_buffer (); }
  212. else { vs += "Unknown"; }
  213. vs += ")";
  214. hInit.setWindowTitle (vs);
  215. }
  216. }
  217. ConfigTable colorTable;
  218. local_populate_color_table (init, hInit, colorTable);
  219. ConfigTable rezTable;
  220. local_populate_resolution_table (init, hInit, rezTable);
  221. local_restore_session (init, hInit);
  222. hInit.show ();
  223. hInit.raise ();
  224. while (hInit.isVisible ()) {
  225. QApplication::sendPostedEvents (0, -1);
  226. QApplication::processEvents (QEventLoop::WaitForMoreEvents);
  227. }
  228. if (init.app.is_running ()) {
  229. local_add_config ("config", init);
  230. Config *colorPtr = colorTable.lookup (
  231. qPrintable (hInit.ui.colorCombo->currentText ()));
  232. if (colorPtr) {
  233. Config fileList;
  234. if (colorPtr->lookup_all_config ("config", fileList)) {
  235. ConfigIterator it;
  236. Config file;
  237. while (fileList.get_next_config (it, file)) {
  238. init.files.append_arg (config_to_string ("file", file));
  239. }
  240. }
  241. }
  242. CommandLine cl;
  243. cl.add_args (init.files);
  244. init.app.process_command_line (cl);
  245. if (!init.app.is_error ()) {
  246. local_setup_resolution (init, hInit, rezTable);
  247. local_set_port (hInit.ui.portBox->value (), init);
  248. }
  249. }
  250. }
  251. };