VirtualBox

Changeset 72136 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 7, 2018 10:01:05 AM (7 years ago)
Author:
vboxsync
Message:

FE/Qt bugref:6769 the first implementation of a widget thru which user can enter a vm name and vm destination path.

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk

    r72109 r72136  
    683683        src/widgets/UIStatusBarEditorWindow.h \
    684684        src/widgets/UIToolBar.h \
     685        src/widgets/UIVMNamePathSelector.h \
    685686        src/widgets/UIWarningPane.h \
    686687        src/wizards/UIWizard.h \
     
    824825        src/widgets/UIStatusBarEditorWindow.h \
    825826        src/widgets/UIToolBar.h \
     827        src/widgets/UIVMNamePathSelector.h \
    826828        src/widgets/UIWarningPane.h \
    827829        src/wizards/UIWizard.h \
     
    12671269        src/widgets/UIStatusBarEditorWindow.cpp \
    12681270        src/widgets/UIToolBar.cpp \
     1271        src/widgets/UIVMNamePathSelector.cpp \
    12691272        src/widgets/UIWarningPane.cpp \
    12701273        src/wizards/UIWizard.cpp \
     
    14351438        src/widgets/UIStatusBarEditorWindow.cpp \
    14361439        src/widgets/UIToolBar.cpp \
     1440        src/widgets/UIVMNamePathSelector.h \
    14371441        src/widgets/UIWarningPane.cpp \
    14381442        src/wizards/UIWizard.cpp \
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIVMNamePathSelector.cpp

    r72101 r72136  
    11/* $Id$ */
    22/** @file
    3  * VBox Qt GUI - UIFilePathSelector class implementation.
     3 * VBox Qt GUI - UIVMNamePathSelector class implementation.
    44 */
    55
     
    2121
    2222/* Qt includes: */
    23 # include <QAction>
    24 # include <QApplication>
    25 # include <QClipboard>
    2623# include <QDir>
    27 # include <QFocusEvent>
    2824# include <QHBoxLayout>
    29 # include <QLineEdit>
    30 # ifdef VBOX_WS_WIN
    31 #  include <QListView>
    32 # endif
    3325
    3426/* GUI includes: */
    35 # include "QIFileDialog.h"
     27# include "QILineEdit.h"
    3628# include "QILabel.h"
    37 # include "QILineEdit.h"
    38 # include "QIToolButton.h"
     29# include "UIVMNamePathSelector.h"
    3930# include "VBoxGlobal.h"
    40 # include "UIIconPool.h"
    41 # include "UIFilePathSelector.h"
    4231
    43 /* Other VBox includes: */
    44 # include <iprt/assert.h>
     32/* COM includes: */
     33# include "CSystemProperties.h"
    4534
    4635#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    4736
    4837
    49 /** Returns first position of difference between passed strings. */
    50 static int differFrom(const QString &str1, const QString &str2)
     38
     39UIVMNamePathSelector::UIVMNamePathSelector(QWidget *pParent /* = 0 */)
     40    : QIWithRetranslateUI<QWidget>(pParent)
     41    , m_pMainLayout(0)
     42    , m_pPath(0)
     43    , m_pName(0)
     44    , m_pSeparator(0)
    5145{
    52     if (str1 == str2)
    53         return -1;
    54 
    55     int iMinLength = qMin(str1.size(), str2.size());
    56     int iIndex = 0;
    57     for (iIndex = 0; iIndex < iMinLength; ++iIndex)
    58         if (str1[iIndex] != str2[iIndex])
    59             break;
    60     return iIndex;
     46    prepareWidgets();
    6147}
    6248
    63 UIFilePathSelector::UIFilePathSelector(QWidget *pParent /* = 0 */)
    64     : QIWithRetranslateUI<QIComboBox>(pParent)
    65     , m_enmMode(Mode_Folder)
    66     , m_strHomeDir(QDir::current().absolutePath())
    67     , m_fEditable(true)
    68     , m_fModified(false)
    69     , m_fEditableMode(false)
    70     , m_fMouseAwaited(false)
    71     , m_fToolTipOverriden(false)
    72     , m_pCopyAction(new QAction(this))
     49QString UIVMNamePathSelector::defaultMachineFolder() const
    7350{
    74 #ifdef VBOX_WS_WIN
    75     // WORKAROUND:
    76     // On at least Windows host there is a bug with
    77     // the QListView which doesn't take into account
    78     // the item size change caused by assigning item's
    79     // icon of another size or unassigning icon at all.
    80     if (view()->inherits("QListView"))
    81         qobject_cast<QListView*>(view())->setUniformItemSizes(true);
    82 #endif /* VBOX_WS_WIN */
     51    /* Get VBox: */
     52    CVirtualBox vbox = vboxGlobal().virtualBox();
     53    /* Get default machine folder: */
     54    /*const QString strDefaultMachineFolder = */return vbox.GetSystemProperties().GetDefaultMachineFolder();
    8355
    84     /* Populate items: */
    85     insertItem(PathId, "");
    86     insertItem(SelectId, "");
    87     insertItem(ResetId, "");
    88 
    89     /* Attaching known icons: */
    90     setItemIcon(SelectId, UIIconPool::iconSet(":/select_file_16px.png"));
    91     setItemIcon(ResetId, UIIconPool::iconSet(":/eraser_16px.png"));
    92 
    93     /* Setup context menu: */
    94     addAction(m_pCopyAction);
    95     m_pCopyAction->setShortcut(QKeySequence(QKeySequence::Copy));
    96     m_pCopyAction->setShortcutContext(Qt::WidgetShortcut);
    97 
    98     /* Initial setup: */
    99     setInsertPolicy(QComboBox::NoInsert);
    100     setContextMenuPolicy(Qt::ActionsContextMenu);
    101     setMinimumWidth(200);
    102 
    103     /* Setup connections: */
    104     connect(this, SIGNAL(activated(int)), this, SLOT(onActivated(int)));
    105     connect(m_pCopyAction, SIGNAL(triggered(bool)), this, SLOT(copyToClipboard()));
    106 
    107     /* Editable by default: */
    108     setEditable(true);
    109 
    110     /* Applying language settings: */
    111     retranslateUi();
    11256}
    11357
    114 void UIFilePathSelector::setMode(Mode enmMode)
     58void UIVMNamePathSelector::prepareWidgets()
    11559{
    116     m_enmMode = enmMode;
     60    m_pMainLayout = new QHBoxLayout;
     61    if (!m_pMainLayout)
     62        return;
     63    m_pMainLayout->setContentsMargins(0, 0, 0, 0);
     64    m_pMainLayout->setContentsMargins(0, 0, 0, 0);
     65    m_pMainLayout->setSpacing(0);
     66    setLayout(m_pMainLayout);
    11767
    118     retranslateUi();
     68    m_pPath = new QILineEdit;
     69    if (m_pPath)
     70    {
     71        m_pMainLayout->addWidget(m_pPath);
     72        m_pPath->setReadOnly(true);
     73        setPath(defaultMachineFolder());
     74    }
     75    m_pSeparator = new QILabel;
     76    if (m_pSeparator)
     77    {
     78        m_pSeparator->setText(QDir::separator());
     79        m_pMainLayout->addWidget(m_pSeparator);
     80    }
     81
     82    m_pName = new QILineEdit;
     83    if (m_pName)
     84    {
     85        m_pMainLayout->addWidget(m_pName);
     86    }
     87
    11988}
    12089
    121 void UIFilePathSelector::setEditable(bool fEditable)
     90QString UIVMNamePathSelector::path() const
    12291{
    123     m_fEditable = fEditable;
    124 
    125     if (m_fEditable)
    126     {
    127         QIComboBox::setEditable(true);
    128 
    129         /* Install combo-box event-filter: */
    130         Assert(comboBox());
    131         comboBox()->installEventFilter(this);
    132 
    133         /* Install line-edit connection/event-filter: */
    134         Assert(lineEdit());
    135         connect(lineEdit(), SIGNAL(textEdited(const QString &)),
    136                 this, SLOT(onTextEdited(const QString &)));
    137         lineEdit()->installEventFilter(this);
    138     }
    139     else
    140     {
    141         if (lineEdit())
    142         {
    143             /* Remove line-edit event-filter/connection: */
    144             lineEdit()->removeEventFilter(this);
    145             disconnect(lineEdit(), SIGNAL(textEdited(const QString &)),
    146                        this, SLOT(onTextEdited(const QString &)));
    147         }
    148         if (comboBox())
    149         {
    150             /* Remove combo-box event-filter: */
    151             comboBox()->removeEventFilter(this);
    152         }
    153         QIComboBox::setEditable(false);
    154     }
     92    if (!m_pPath)
     93        return QString();
     94    return m_pPath->text();
    15595}
    15696
    157 void UIFilePathSelector::setResetEnabled(bool fEnabled)
     97void UIVMNamePathSelector::setPath(const QString &path)
    15898{
    159     if (!fEnabled && count() - 1 == ResetId)
    160         removeItem(ResetId);
    161     else if (fEnabled && count() - 1 == ResetId - 1)
    162     {
    163         insertItem(ResetId, "");
    164         setItemIcon(ResetId, UIIconPool::iconSet(":/eraser_16px.png"));
    165     }
    166     retranslateUi();
     99    if (!m_pPath || m_pPath->text() == path)
     100        return;
     101    m_pPath->setText(path);
     102    m_pPath->setFixedWidthByText(path);
    167103}
    168104
    169 void UIFilePathSelector::setToolTip(const QString &strToolTip)
     105QString UIVMNamePathSelector::name() const
    170106{
    171     /* Call to base-class: */
    172     QIComboBox::setToolTip(strToolTip);
     107    if (!m_pName)
     108        return QString();
     109    return m_pName->text();
    173110
    174     /* Remember if the tool-tip overriden: */
    175     m_fToolTipOverriden = !toolTip().isEmpty();
    176111}
    177112
    178 void UIFilePathSelector::setPath(const QString &strPath, bool fRefreshText /* = true */)
     113void UIVMNamePathSelector::setName(const QString &name)
    179114{
    180     m_strPath = strPath.isEmpty() ? QString::null :
    181             QDir::toNativeSeparators(strPath);
    182     if (fRefreshText)
    183         refreshText();
     115    if (!m_pName && m_pName->text() == name)
     116        return;
     117    m_pName->setText(name);
    184118}
    185119
    186 bool UIFilePathSelector::eventFilter(QObject *pObject, QEvent *pEvent)
     120
     121void UIVMNamePathSelector::retranslateUi()
    187122{
    188     /* If the object is private combo-box: */
    189     if (pObject == comboBox())
    190     {
    191         /* Handle focus events related to private child: */
    192         switch (pEvent->type())
    193         {
    194             case QEvent::FocusIn:  focusInEvent(static_cast<QFocusEvent*>(pEvent)); break;
    195             case QEvent::FocusOut: focusOutEvent(static_cast<QFocusEvent*>(pEvent)); break;
    196             default: break;
    197         }
    198     }
    199 
    200     /* If the object is private line-edit: */
    201     if (pObject == lineEdit())
    202     {
    203         if (m_fMouseAwaited && (pEvent->type() == QEvent::MouseButtonPress))
    204             QMetaObject::invokeMethod(this, "refreshText", Qt::QueuedConnection);
    205     }
    206 
    207     /* Call to base-class: */
    208     return QIWithRetranslateUI<QIComboBox>::eventFilter(pObject, pEvent);
    209123}
    210 
    211 void UIFilePathSelector::resizeEvent(QResizeEvent *pEvent)
    212 {
    213     QIWithRetranslateUI<QIComboBox>::resizeEvent(pEvent);
    214     refreshText();
    215 }
    216 
    217 void UIFilePathSelector::focusInEvent(QFocusEvent *pEvent)
    218 {
    219     if (isPathSelected())
    220     {
    221         if (m_fEditable)
    222             m_fEditableMode = true;
    223         if (pEvent->reason() == Qt::MouseFocusReason)
    224             m_fMouseAwaited = true;
    225         else
    226             refreshText();
    227     }
    228     QIWithRetranslateUI<QIComboBox>::focusInEvent(pEvent);
    229 }
    230 
    231 void UIFilePathSelector::focusOutEvent(QFocusEvent *pEvent)
    232 {
    233     if (isPathSelected())
    234     {
    235         m_fEditableMode = false;
    236         refreshText();
    237     }
    238     QIWithRetranslateUI<QIComboBox>::focusOutEvent(pEvent);
    239 }
    240 
    241 void UIFilePathSelector::retranslateUi()
    242 {
    243     /* Retranslate copy action: */
    244     m_pCopyAction->setText(tr("&Copy"));
    245 
    246     /* Retranslate 'select' item: */
    247     setItemText(SelectId, tr("Other..."));
    248 
    249     /* Retranslate 'reset' item: */
    250     if (count() - 1 == ResetId)
    251         setItemText(ResetId, tr("Reset"));
    252 
    253     /* Set tool-tips of the above two items based on the mode: */
    254     switch (m_enmMode)
    255     {
    256         case Mode_Folder:
    257             setItemData(SelectId,
    258                         tr("Displays a window to select a different folder."),
    259                         Qt::ToolTipRole);
    260             setItemData(ResetId,
    261                         tr("Resets the folder path to the default value."),
    262                         Qt::ToolTipRole);
    263             break;
    264         case Mode_File_Open:
    265         case Mode_File_Save:
    266             setItemData(SelectId,
    267                         tr("Displays a window to select a different file."),
    268                         Qt::ToolTipRole);
    269             setItemData(ResetId,
    270                         tr("Resets the file path to the default value."),
    271                         Qt::ToolTipRole);
    272             break;
    273         default:
    274             AssertFailedBreak();
    275     }
    276 
    277     /* If selector is NOT focused => we interpret the "nothing selected"
    278      * item depending on "reset to default" feature state: */
    279     if (isResetEnabled())
    280     {
    281         /* If "reset to default" is enabled: */
    282         m_strNoneText = tr("<reset to default>");
    283         m_strNoneToolTip = tr("The actual default path value will be displayed after "
    284                               "accepting the changes and opening this window again.");
    285     }
    286     else
    287     {
    288         /* If "reset to default" is NOT enabled: */
    289         m_strNoneText = tr("<not selected>");
    290         m_strNoneToolTip = tr("Please use the <b>Other...</b> item from the drop-down "
    291                               "list to select a path.");
    292     }
    293 
    294     /* But if selector is focused => tool-tip depends on the mode only: */
    295     switch (m_enmMode)
    296     {
    297         case Mode_Folder:
    298             m_strNoneToolTipFocused = tr("Holds the folder path.");
    299             break;
    300         case Mode_File_Open:
    301         case Mode_File_Save:
    302             m_strNoneToolTipFocused = tr("Holds the file path.");
    303             break;
    304         default:
    305             AssertFailedBreak();
    306     }
    307 
    308     /* Finally, retranslate current item: */
    309     refreshText();
    310 }
    311 
    312 void UIFilePathSelector::onActivated(int iIndex)
    313 {
    314     switch (iIndex)
    315     {
    316         case SelectId:
    317         {
    318             selectPath();
    319             break;
    320         }
    321         case ResetId:
    322         {
    323             changePath(QString::null);
    324             break;
    325         }
    326         default:
    327             break;
    328     }
    329     setCurrentIndex(PathId);
    330     setFocus();
    331 }
    332 
    333 void UIFilePathSelector::onTextEdited(const QString &strPath)
    334 {
    335     changePath(strPath, false /* refresh text? */);
    336 }
    337 
    338 void UIFilePathSelector::copyToClipboard()
    339 {
    340     QString text(fullPath());
    341     /* Copy the current text to the selection and global clipboard. */
    342     if (QApplication::clipboard()->supportsSelection())
    343         QApplication::clipboard()->setText(text, QClipboard::Selection);
    344     QApplication::clipboard()->setText(text, QClipboard::Clipboard);
    345 }
    346 
    347 void UIFilePathSelector::changePath(const QString &strPath,
    348                                     bool fRefreshText /* = true */)
    349 {
    350     const QString strOldPath = m_strPath;
    351     setPath(strPath, fRefreshText);
    352     if (!m_fModified && m_strPath != strOldPath)
    353         m_fModified = true;
    354     emit pathChanged(strPath);
    355 }
    356 
    357 void UIFilePathSelector::selectPath()
    358 {
    359     /* Prepare initial directory: */
    360     QString strInitDir;
    361     /* If something already chosen: */
    362     if (!m_strPath.isEmpty())
    363     {
    364         /* If that is just a single file/folder (object) name: */
    365         const QString strObjectName = QFileInfo(m_strPath).fileName();
    366         if (strObjectName == m_strPath)
    367         {
    368             /* Use the home directory: */
    369             strInitDir = m_strHomeDir;
    370         }
    371         /* If that is full file/folder (object) path: */
    372         else
    373         {
    374             /* Use the first existing dir of m_strPath: */
    375             strInitDir = QIFileDialog::getFirstExistingDir(m_strPath);
    376         }
    377         /* Finally, append object name itself: */
    378         strInitDir = QDir(strInitDir).absoluteFilePath(strObjectName);
    379     }
    380     /* Use the home directory by default: */
    381     if (strInitDir.isNull())
    382         strInitDir = m_strHomeDir;
    383 
    384     /* Open the choose-file/folder dialog: */
    385     QString strSelPath;
    386     switch (m_enmMode)
    387     {
    388         case Mode_File_Open:
    389             strSelPath = QIFileDialog::getOpenFileName(strInitDir, m_strFileDialogFilters, parentWidget(), m_strFileDialogTitle); break;
    390         case Mode_File_Save:
    391         {
    392             strSelPath = QIFileDialog::getSaveFileName(strInitDir, m_strFileDialogFilters, parentWidget(), m_strFileDialogTitle);
    393             if (!strSelPath.isEmpty() && QFileInfo(strSelPath).suffix().isEmpty())
    394             {
    395                 if (m_strFileDialogDefaultSaveExtension.isEmpty())
    396                     strSelPath = QString("%1").arg(strSelPath);
    397                 else
    398                     strSelPath = QString("%1.%2").arg(strSelPath).arg(m_strFileDialogDefaultSaveExtension);
    399             }
    400             break;
    401         }
    402         case Mode_Folder:
    403             strSelPath = QIFileDialog::getExistingDirectory(strInitDir, parentWidget(), m_strFileDialogTitle); break;
    404     }
    405 
    406     /* Do nothing if nothing chosen: */
    407     if (strSelPath.isNull())
    408         return;
    409 
    410     /* Wipe out excessive slashes: */
    411     strSelPath.remove(QRegExp("[\\\\/]$"));
    412 
    413     /* Apply chosen path: */
    414     changePath(strSelPath);
    415 }
    416 
    417 QIcon UIFilePathSelector::defaultIcon() const
    418 {
    419     if (m_enmMode == Mode_Folder)
    420         return vboxGlobal().icon(QFileIconProvider::Folder);
    421     else
    422         return vboxGlobal().icon(QFileIconProvider::File);
    423 }
    424 
    425 QString UIFilePathSelector::fullPath(bool fAbsolute /* = true */) const
    426 {
    427     if (m_strPath.isNull())
    428         return m_strPath;
    429 
    430     QString strResult;
    431     switch (m_enmMode)
    432     {
    433         case Mode_Folder:
    434             strResult = fAbsolute ? QDir(m_strPath).absolutePath() :
    435                                     QDir(m_strPath).path();
    436             break;
    437         case Mode_File_Open:
    438         case Mode_File_Save:
    439             strResult = fAbsolute ? QFileInfo(m_strPath).absoluteFilePath() :
    440                                     QFileInfo(m_strPath).filePath();
    441             break;
    442         default:
    443             AssertFailedBreak();
    444     }
    445     return QDir::toNativeSeparators(strResult);
    446 }
    447 
    448 QString UIFilePathSelector::shrinkText(int iWidth) const
    449 {
    450     QString strFullText(fullPath(false));
    451     if (strFullText.isEmpty())
    452         return strFullText;
    453 
    454     int iOldSize = fontMetrics().width(strFullText);
    455     int iIndentSize = fontMetrics().width("x...x");
    456 
    457     /* Compress text: */
    458     int iStart = 0;
    459     int iFinish = 0;
    460     int iPosition = 0;
    461     int iTextWidth = 0;
    462     do {
    463         iTextWidth = fontMetrics().width(strFullText);
    464         if (iTextWidth + iIndentSize > iWidth)
    465         {
    466             iStart = 0;
    467             iFinish = strFullText.length();
    468 
    469             /* Selecting remove position: */
    470             QRegExp regExp("([\\\\/][^\\\\^/]+[\\\\/]?$)");
    471             int iNewFinish = regExp.indexIn(strFullText);
    472             if (iNewFinish != -1)
    473                 iFinish = iNewFinish;
    474             iPosition = (iFinish - iStart) / 2;
    475 
    476             if (iPosition == iFinish)
    477                break;
    478 
    479             strFullText.remove(iPosition, 1);
    480         }
    481     } while (iTextWidth + iIndentSize > iWidth);
    482 
    483     strFullText.insert(iPosition, "...");
    484     int newSize = fontMetrics().width(strFullText);
    485 
    486     return newSize < iOldSize ? strFullText : fullPath(false);
    487 }
    488 
    489 void UIFilePathSelector::refreshText()
    490 {
    491     if (m_fEditable && m_fEditableMode)
    492     {
    493         /* Cursor positioning variables: */
    494         int iCurPos = -1;
    495         int iDiffPos = -1;
    496         int iFromRight = -1;
    497 
    498         if (m_fMouseAwaited)
    499         {
    500             /* Store the cursor position: */
    501             iCurPos = lineEdit()->cursorPosition();
    502             iDiffPos = differFrom(lineEdit()->text(), m_strPath);
    503             iFromRight = lineEdit()->text().size() - iCurPos;
    504         }
    505 
    506         /* In editable mode there should be no any icon
    507          * and text have be corresponding real stored path
    508          * which can be absolute or relative. */
    509         if (lineEdit()->text() != m_strPath)
    510             setItemText(PathId, m_strPath);
    511         setItemIcon(PathId, QIcon());
    512 
    513         /* Set the tool-tip: */
    514         if (!m_fToolTipOverriden)
    515             QIComboBox::setToolTip(m_strNoneToolTipFocused);
    516         setItemData(PathId, toolTip(), Qt::ToolTipRole);
    517 
    518         if (m_fMouseAwaited)
    519         {
    520             m_fMouseAwaited = false;
    521 
    522             /* Restore the position to the right of dots: */
    523             if (iDiffPos != -1 && iCurPos >= iDiffPos + 3)
    524                 lineEdit()->setCursorPosition(lineEdit()->text().size() -
    525                                               iFromRight);
    526             /* Restore the position to the center of text: */
    527             else if (iDiffPos != -1 && iCurPos > iDiffPos)
    528                 lineEdit()->setCursorPosition(lineEdit()->text().size() / 2);
    529             /* Restore the position to the left of dots: */
    530             else
    531                 lineEdit()->setCursorPosition(iCurPos);
    532         }
    533     }
    534     else if (m_strPath.isNull())
    535     {
    536         /* If we are not in editable mode and no path is
    537          * stored here - show the translated 'none' string. */
    538         if (itemText(PathId) != m_strNoneText)
    539         {
    540             setItemText(PathId, m_strNoneText);
    541             setItemIcon(PathId, QIcon());
    542 
    543             /* Set the tool-tip: */
    544             if (!m_fToolTipOverriden)
    545                 QIComboBox::setToolTip(m_strNoneToolTip);
    546             setItemData(PathId, toolTip(), Qt::ToolTipRole);
    547         }
    548     }
    549     else
    550     {
    551         /* Compress text in combobox: */
    552         QStyleOptionComboBox options;
    553         options.initFrom(this);
    554         QRect rect = QApplication::style()->subControlRect(
    555             QStyle::CC_ComboBox, &options, QStyle::SC_ComboBoxEditField);
    556         setItemText(PathId, shrinkText(rect.width() - iconSize().width()));
    557 
    558         /* Attach corresponding icon: */
    559         setItemIcon(PathId, QFileInfo(m_strPath).exists() ?
    560                             vboxGlobal().icon(QFileInfo(m_strPath)) :
    561                             defaultIcon());
    562 
    563         /* Set the tool-tip: */
    564         if (!m_fToolTipOverriden)
    565             QIComboBox::setToolTip(fullPath());
    566         setItemData(PathId, toolTip(), Qt::ToolTipRole);
    567     }
    568 }
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIVMNamePathSelector.h

    r72101 r72136  
    11/* $Id$ */
    22/** @file
    3  * VBox Qt GUI - UIFilePathSelector class declaration.
     3 * VBox Qt GUI - UIVMNamePathSelector class declaration.
    44 */
    55
     
    1616 */
    1717
    18 #ifndef ___UIFilePathSelector_h___
    19 #define ___UIFilePathSelector_h___
     18#ifndef ___UIVMNamePathSelector_h___
     19#define ___UIVMNamePathSelector_h___
     20
     21/* Qt includes: */
     22#include <QLineEdit>
    2023
    2124/* GUI includes: */
    22 #include "QIComboBox.h"
    2325#include "QIWithRetranslateUI.h"
    24 #include "UILibraryDefs.h"
    2526
    2627/* Forward declarations: */
    27 class QAction;
    28 class QFocusEvent;
    2928class QHBoxLayout;
    30 class QObject;
    31 class QResizeEvent;
    32 class QWidget;
    33 class QString;
    3429class QILabel;
    3530class QILineEdit;
    36 class QIToolButton;
    3731
    38 /** QIComboBox subclass providing GUI with the
    39   * possibility to choose/reflect file/folder path. */
    40 class SHARED_LIBRARY_STUFF UIFilePathSelector : public QIWithRetranslateUI<QIComboBox>
     32
     33class SHARED_LIBRARY_STUFF UIVMNamePathSelector : public QIWithRetranslateUI<QWidget>
    4134{
    4235    Q_OBJECT;
     
    4437signals:
    4538
    46     /** Notify listeners about @a strPath changed. */
    47     void pathChanged(const QString &strPath);
    48 
    4939public:
    5040
    51     /** Modes file-path selector operates in. */
    52     enum Mode
    53     {
    54         Mode_Folder = 0,
    55         Mode_File_Open,
    56         Mode_File_Save
    57     };
    58 
    59     /** Combo-box field IDs file-path selector uses. */
    60     enum
    61     {
    62         PathId = 0,
    63         SelectId,
    64         ResetId
    65     };
    66 
    67     /** Constructs file-path selector passing @a pParent to QIComboBox base-class. */
    68     UIFilePathSelector(QWidget *pParent = 0);
    69 
    70     /** Defines the @a enmMode to operate in. */
    71     void setMode(Mode enmMode);
    72     /** Returns the mode to operate in. */
    73     Mode mode() const { return m_enmMode; }
    74 
    75     /** Defines whether the path is @a fEditable. */
    76     void setEditable(bool fEditable);
    77     /** Returns whether the path is editable. */
    78     bool isEditable() const { return m_fEditable; }
    79 
    80     /** Defines whether the reseting to defauilt path is @a fEnabled. */
    81     void setResetEnabled(bool fEnabled);
    82     /** Returns whether the reseting to defauilt path is enabled. */
    83     bool isResetEnabled() const { return count() - 1  == ResetId; }
    84 
    85     /** Defines the file-dialog @a strTitle. */
    86     void setFileDialogTitle(const QString &strTitle) { m_strFileDialogTitle = strTitle; }
    87     /** Returns the file-dialog title. */
    88     QString fileDialogTitle() const { return m_strFileDialogTitle; }
    89 
    90     /** Defines the file-dialog @a strFilters. */
    91     void setFileDialogFilters(const QString &strFilters) { m_strFileDialogFilters = strFilters; }
    92     /** Returns the file-dialog filters. */
    93     QString fileDialogFilters() const { return m_strFileDialogFilters; }
    94 
    95     /** Defines the file-dialog @a strDefaultSaveExtension. */
    96     void setFileDialogDefaultSaveExtension(const QString &strDefaultSaveExtension) { m_strFileDialogDefaultSaveExtension = strDefaultSaveExtension; }
    97     /** Returns the file-dialog default save extension. */
    98     QString fileDialogDefaultSaveExtension() const { return m_strFileDialogDefaultSaveExtension; }
    99 
    100     /** Resets path modified state to false. */
    101     void resetModified() { m_fModified = false; }
    102     /** Returns whether the path is modified. */
    103     bool isModified() const { return m_fModified; }
    104     /** Returns whether the path is selected. */
    105     bool isPathSelected() const { return currentIndex() == PathId; }
    106 
    107     /** Returns the path. */
    108     QString path() const { return m_strPath; }
    109 
    110     /** Sets overriden widget's @a strToolTip.
    111       * @note If nothing set it's generated automatically. */
    112     void setToolTip(const QString &strToolTip);
     41    UIVMNamePathSelector(QWidget *pParent = 0);
    11342
    11443public slots:
    11544
    116     /** Defines the @a strPath and @a fRefreshText after that. */
    117     void setPath(const QString &strPath, bool fRefreshText = true);
     45    QString path() const;
     46    void    setPath(const QString &path);
    11847
    119     /** Defines the @a strHomeDir. */
    120     void setHomeDir(const QString &strHomeDir) { m_strHomeDir = strHomeDir; }
     48    QString name() const;
     49    void    setName(const QString &name);
    12150
    12251protected:
    12352
    124     /** Preprocesses every @a pEvent sent to @a pObject. */
    125     bool eventFilter(QObject *pObject, QEvent *pEvent);
    126 
    127     /** Handles resize @a pEvent. */
    128     void resizeEvent(QResizeEvent *pEvent);
    129 
    130     /** Handles focus-in @a pEvent. */
    131     void focusInEvent(QFocusEvent *pEvent);
    132     /** Handles focus-out @a pEvent. */
    133     void focusOutEvent(QFocusEvent *pEvent);
    134 
    135     /** Handles translation event. */
    136     void retranslateUi();
     53    void retranslateUi() /* override */;
    13754
    13855private slots:
    13956
    140     /** Handles combo-box @a iIndex activation. */
    141     void onActivated(int iIndex);
    142 
    143     /** Handles combo-box @a strText editing. */
    144     void onTextEdited(const QString &strText);
    145 
    146     /** Handles combo-box text copying. */
    147     void copyToClipboard();
    148 
    149     /** Refreshes combo-box text according to chosen path. */
    150     void refreshText();
    15157
    15258private:
    15359
    154     /** Provokes change to @a strPath and @a fRefreshText after that. */
    155     void changePath(const QString &strPath, bool fRefreshText = true);
     60    void         prepareWidgets();
     61    QString      defaultMachineFolder() const;
    15662
    157     /** Call for file-dialog to choose path. */
    158     void selectPath();
    159 
    160     /** Returns default icon. */
    161     QIcon defaultIcon() const;
    162 
    163     /** Returns full path @a fAbsolute if necessary. */
    164     QString fullPath(bool fAbsolute = true) const;
    165 
    166     /** Shrinks the reflected text to @a iWidth pixels. */
    167     QString shrinkText(int iWidth) const;
    168 
    169     /** Holds the mode to operate in. */
    170     Mode     m_enmMode;
    171 
    172     /** Holds the path. */
    173     QString  m_strPath;
    174     /** Holds the home dir. */
    175     QString  m_strHomeDir;
    176 
    177     /** Holds the file-dialog title. */
    178     QString  m_strFileDialogTitle;
    179     /** Holds the file-dialog filters. */
    180     QString  m_strFileDialogFilters;
    181     /** Holds the file-dialog default save extension. */
    182     QString  m_strFileDialogDefaultSaveExtension;
    183 
    184     /** Holds the cached text for empty path. */
    185     QString  m_strNoneText;
    186     /** Holds the cached tool-tip for empty path. */
    187     QString  m_strNoneToolTip;
    188     /** Holds the cached tool-tip for empty path in focused case. */
    189     QString  m_strNoneToolTipFocused;
    190 
    191     /** Holds whether the path is editable. */
    192     bool     m_fEditable;
    193     /** Holds whether the path is modified. */
    194     bool     m_fModified;
    195 
    196     /** Holds whether we are in editable mode. */
    197     bool     m_fEditableMode;
    198     /** Holds whether we are expecting mouse events. */
    199     bool     m_fMouseAwaited;
    200 
    201     /** Holds whether the tool-tip overriden. */
    202     bool     m_fToolTipOverriden;
    203 
    204     /** Holds the copy action instance. */
    205     QAction *m_pCopyAction;
     63    QHBoxLayout *m_pMainLayout;
     64    QILineEdit  *m_pPath;
     65    QILineEdit  *m_pName;
     66    QILabel     *m_pSeparator;
    20667};
    20768
    208 #endif /* !___UIFilePathSelector_h___ */
     69#endif /* !___UIVMNamePathSelector_h___ */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette