PageRenderTime 63ms CodeModel.GetById 15ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

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