/src/VBox/Frontends/VirtualBox/src/settings/vm/VBoxVMSettingsParallel.cpp

https://gitlab.com/ufo/virtualbox-ose-3-1-8 · C++ · 261 lines · 191 code · 35 blank · 35 comment · 14 complexity · 54f2c6b991459a1e60b0ea45b03a35ed MD5 · raw file

  1. /** @file
  2. *
  3. * VBox frontends: Qt4 GUI ("VirtualBox"):
  4. * VBoxVMSettingsParallel class implementation
  5. */
  6. /*
  7. * Copyright (C) 2006-2008 Sun Microsystems, Inc.
  8. *
  9. * This file is part of VirtualBox Open Source Edition (OSE), as
  10. * available from http://www.virtualbox.org. This file is free software;
  11. * you can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GPL) as published by the Free Software
  13. * Foundation, in version 2 as it comes in the "COPYING" file of the
  14. * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  15. * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  16. *
  17. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
  18. * Clara, CA 95054 USA or visit http://www.sun.com if you need
  19. * additional information or have any questions.
  20. */
  21. #include "VBoxVMSettingsParallel.h"
  22. #include "QIWidgetValidator.h"
  23. #include "VBoxGlobal.h"
  24. #include <QDir>
  25. /* VBoxVMSettingsParallel stuff */
  26. VBoxVMSettingsParallel::VBoxVMSettingsParallel()
  27. : QIWithRetranslateUI<QWidget> (0)
  28. , mValidator (0)
  29. {
  30. /* Apply UI decorations */
  31. Ui::VBoxVMSettingsParallel::setupUi (this);
  32. /* Setup validation */
  33. mLeIRQ->setValidator (new QIULongValidator (0, 255, this));
  34. mLeIOPort->setValidator (new QIULongValidator (0, 0xFFFF, this));
  35. mLePath->setValidator (new QRegExpValidator (QRegExp (".+"), this));
  36. /* Setup constraints */
  37. mLeIRQ->setFixedWidth (mLeIRQ->fontMetrics().width ("8888"));
  38. mLeIOPort->setFixedWidth (mLeIOPort->fontMetrics().width ("8888888"));
  39. /* Set initial values */
  40. /* Note: If you change one of the following don't forget retranslateUi. */
  41. mCbNumber->insertItem (0, vboxGlobal().toCOMPortName (0, 0));
  42. mCbNumber->insertItems (0, vboxGlobal().COMPortNames());
  43. /* Setup connections */
  44. connect (mGbParallel, SIGNAL (toggled (bool)),
  45. this, SLOT (mGbParallelToggled (bool)));
  46. connect (mCbNumber, SIGNAL (activated (const QString &)),
  47. this, SLOT (mCbNumberActivated (const QString &)));
  48. /* Applying language settings */
  49. retranslateUi();
  50. }
  51. void VBoxVMSettingsParallel::getFromPort (const CParallelPort &aPort)
  52. {
  53. mPort = aPort;
  54. mGbParallel->setChecked (mPort.GetEnabled());
  55. ulong IRQ = mPort.GetIRQ();
  56. ulong IOBase = mPort.GetIOBase();
  57. mCbNumber->setCurrentIndex (mCbNumber->
  58. findText (vboxGlobal().toCOMPortName (IRQ, IOBase)));
  59. mLeIRQ->setText (QString::number (IRQ));
  60. mLeIOPort->setText ("0x" + QString::number (IOBase, 16).toUpper());
  61. mLePath->setText (mPort.GetPath());
  62. /* Ensure everything is up-to-date */
  63. mGbParallelToggled (mGbParallel->isChecked());
  64. }
  65. void VBoxVMSettingsParallel::putBackToPort()
  66. {
  67. mPort.SetEnabled (mGbParallel->isChecked());
  68. mPort.SetIRQ (mLeIRQ->text().toULong (NULL, 0));
  69. mPort.SetIOBase (mLeIOPort->text().toULong (NULL, 0));
  70. mPort.SetPath (QDir::toNativeSeparators (mLePath->text()));
  71. }
  72. void VBoxVMSettingsParallel::setValidator (QIWidgetValidator *aVal)
  73. {
  74. Assert (aVal);
  75. mValidator = aVal;
  76. connect (mLeIRQ, SIGNAL (textChanged (const QString &)),
  77. mValidator, SLOT (revalidate()));
  78. connect (mLeIOPort, SIGNAL (textChanged (const QString &)),
  79. mValidator, SLOT (revalidate()));
  80. connect (mLePath, SIGNAL (textChanged (const QString &)),
  81. mValidator, SLOT (revalidate()));
  82. mValidator->revalidate();
  83. }
  84. QWidget* VBoxVMSettingsParallel::setOrderAfter (QWidget *aAfter)
  85. {
  86. setTabOrder (aAfter, mGbParallel);
  87. setTabOrder (mGbParallel, mCbNumber);
  88. setTabOrder (mCbNumber, mLeIRQ);
  89. setTabOrder (mLeIRQ, mLeIOPort);
  90. setTabOrder (mLeIOPort, mLePath);
  91. return mLePath;
  92. }
  93. QString VBoxVMSettingsParallel::pageTitle() const
  94. {
  95. QString pageTitle;
  96. if (!mPort.isNull())
  97. pageTitle = QString (tr ("Port %1", "parallel ports"))
  98. .arg (QString ("&%1").arg (mPort.GetSlot() + 1));
  99. return pageTitle;
  100. }
  101. bool VBoxVMSettingsParallel::isUserDefined()
  102. {
  103. ulong a, b;
  104. return !vboxGlobal().toCOMPortNumbers (mCbNumber->currentText(), a, b);
  105. }
  106. void VBoxVMSettingsParallel::retranslateUi()
  107. {
  108. /* Translate uic generated strings */
  109. Ui::VBoxVMSettingsParallel::retranslateUi (this);
  110. mCbNumber->setItemText (mCbNumber->count() - 1, vboxGlobal().toCOMPortName (0, 0));
  111. }
  112. void VBoxVMSettingsParallel::mGbParallelToggled (bool aOn)
  113. {
  114. if (aOn)
  115. mCbNumberActivated (mCbNumber->currentText());
  116. if (mValidator)
  117. mValidator->revalidate();
  118. }
  119. void VBoxVMSettingsParallel::mCbNumberActivated (const QString &aText)
  120. {
  121. ulong IRQ, IOBase;
  122. bool std = vboxGlobal().toCOMPortNumbers (aText, IRQ, IOBase);
  123. mLeIRQ->setEnabled (!std);
  124. mLeIOPort->setEnabled (!std);
  125. if (std)
  126. {
  127. mLeIRQ->setText (QString::number (IRQ));
  128. mLeIOPort->setText ("0x" + QString::number (IOBase, 16).toUpper());
  129. }
  130. }
  131. /* VBoxVMSettingsParallelPage stuff */
  132. VBoxVMSettingsParallelPage::VBoxVMSettingsParallelPage()
  133. : mValidator (0)
  134. {
  135. /* TabWidget creation */
  136. mTabWidget = new QTabWidget (this);
  137. QVBoxLayout *layout = new QVBoxLayout (this);
  138. layout->setContentsMargins (0, 5, 0, 5);
  139. layout->addWidget (mTabWidget);
  140. }
  141. void VBoxVMSettingsParallelPage::getFrom (const CMachine &aMachine)
  142. {
  143. Assert (mFirstWidget);
  144. setTabOrder (mFirstWidget, mTabWidget->focusProxy());
  145. QWidget *lastFocusWidget = mTabWidget->focusProxy();
  146. /* Tab pages loading */
  147. ulong count = vboxGlobal().virtualBox().
  148. GetSystemProperties().GetParallelPortCount();
  149. for (ulong slot = 0; slot < count; ++ slot)
  150. {
  151. CParallelPort port = aMachine.GetParallelPort (slot);
  152. VBoxVMSettingsParallel *page = new VBoxVMSettingsParallel();
  153. page->getFromPort (port);
  154. mTabWidget->addTab (page, page->pageTitle());
  155. Assert (mValidator);
  156. page->setValidator (mValidator);
  157. lastFocusWidget = page->setOrderAfter (lastFocusWidget);
  158. }
  159. }
  160. void VBoxVMSettingsParallelPage::putBackTo()
  161. {
  162. for (int index = 0; index < mTabWidget->count(); ++ index)
  163. {
  164. VBoxVMSettingsParallel *page =
  165. (VBoxVMSettingsParallel*) mTabWidget->widget (index);
  166. Assert (page);
  167. page->putBackToPort();
  168. }
  169. }
  170. void VBoxVMSettingsParallelPage::setValidator (QIWidgetValidator *aVal)
  171. {
  172. mValidator = aVal;
  173. }
  174. bool VBoxVMSettingsParallelPage::revalidate (QString &aWarning, QString &aTitle)
  175. {
  176. bool valid = true;
  177. QStringList ports;
  178. QStringList paths;
  179. int index = 0;
  180. for (; index < mTabWidget->count(); ++ index)
  181. {
  182. QWidget *tab = mTabWidget->widget (index);
  183. VBoxVMSettingsParallel *page =
  184. static_cast<VBoxVMSettingsParallel*> (tab);
  185. /* Check the predefined port number unicity */
  186. if (page->mGbParallel->isChecked() && !page->isUserDefined())
  187. {
  188. QString port = page->mCbNumber->currentText();
  189. valid = !ports.contains (port);
  190. if (!valid)
  191. {
  192. aWarning = tr ("Duplicate port number selected ");
  193. aTitle += ": " +
  194. vboxGlobal().removeAccelMark (mTabWidget->tabText (mTabWidget->indexOf (tab)));
  195. break;
  196. }
  197. ports << port;
  198. }
  199. /* Check the port path emptiness & unicity */
  200. if (page->mGbParallel->isChecked())
  201. {
  202. QString path = page->mLePath->text();
  203. valid = !path.isEmpty() && !paths.contains (path);
  204. if (!valid)
  205. {
  206. aWarning = path.isEmpty() ?
  207. tr ("Port path not specified ") :
  208. tr ("Duplicate port path entered ");
  209. aTitle += ": " +
  210. vboxGlobal().removeAccelMark (mTabWidget->tabText (mTabWidget->indexOf (tab)));
  211. break;
  212. }
  213. paths << path;
  214. }
  215. }
  216. return valid;
  217. }
  218. void VBoxVMSettingsParallelPage::retranslateUi()
  219. {
  220. for (int i = 0; i < mTabWidget->count(); ++ i)
  221. {
  222. VBoxVMSettingsParallel *page =
  223. static_cast<VBoxVMSettingsParallel*> (mTabWidget->widget (i));
  224. mTabWidget->setTabText (i, page->pageTitle());
  225. }
  226. }