VirtualBox

Changeset 67262 in vbox


Ignore:
Timestamp:
Jun 5, 2017 4:37:11 PM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
115957
Message:

FE/Qt: Selector UI: Tools pane: Snapshot pane: Rework snapshot tree to have additional 'Taken' column.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/selector
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/selector/UISnapshotPane.cpp

    r67261 r67262  
    5858
    5959
     60/** Snapshot tree column tags. */
     61enum
     62{
     63    Column_Name,
     64    Column_Taken,
     65    Column_Max,
     66};
     67
     68
    6069/** QITreeWidgetItem subclass for snapshots items. */
    6170class UISnapshotItem : public QITreeWidgetItem, public UIDataSnapshot
     
    8796    QString snapshotID() const { return m_strSnapshotID; }
    8897
    89     /** Returns item data for corresponding @a iColumn and @a iRole. */
    90     QVariant data(int iColumn, int iRole) const;
    91 
    92     /** Returns item text for corresponding @a iColumn. */
    93     QString text(int iColumn) const;
    94 
    9598    /** Returns whether this is the "current state" item. */
    9699    bool isCurrentStateItem() const { return m_fCurrentStateItem; }
     
    116119
    117120protected:
    118 
    119     /** Adjusts item text. */
    120     void adjustText();
    121121
    122122    /** Recaches item tool-tip. */
     
    220220    /* Set the bold font state
    221221     * for current state item: */
    222     QFont myFont = font(0);
     222    QFont myFont = font(Column_Name);
    223223    myFont.setBold(true);
    224     setFont(0, myFont);
     224    setFont(Column_Name, myFont);
    225225
    226226    /* Fetch current machine state: */
     
    237237    /* Set the bold font state
    238238     * for current state item: */
    239     QFont myFont = font(0);
     239    QFont myFont = font(Column_Name);
    240240    myFont.setBold(true);
    241     setFont(0, myFont);
     241    setFont(Column_Name, myFont);
    242242
    243243    /* Fetch current machine state: */
    244244    setMachineState(m_comMachine.GetState());
    245 }
    246 
    247 QVariant UISnapshotItem::data(int iColumn, int iRole) const
    248 {
    249     switch (iRole)
    250     {
    251         case Qt::DisplayRole:
    252         {
    253             /* Call to base-class for "current state" item, compose ourselves otherwise: */
    254             return m_fCurrentStateItem ? QTreeWidgetItem::data(iColumn, iRole)
    255                                        : QString("%1%2")
    256                                              .arg(QTreeWidgetItem::data(iColumn, Qt::DisplayRole).toString())
    257                                              .arg(QTreeWidgetItem::data(iColumn, Qt::UserRole).toString());
    258         }
    259         case Qt::SizeHintRole:
    260         {
    261             /* Determine the icon metric: */
    262             const QStyle *pStyle = QApplication::style();
    263             const int iIconMetric = pStyle->pixelMetric(QStyle::PM_SmallIconSize);
    264             /* Determine the minimum size-hint for this tree-widget-item: */
    265             const QSize baseSizeHint = QTreeWidgetItem::data(iColumn, iRole).toSize();
    266             /* Determine the effective height-hint for this tree-widget-item: */
    267             const int iEffectiveHeightHint = qMax(baseSizeHint.height(),
    268                                                   iIconMetric + 2 * 2 /* margins */);
    269             /* Return size-hint for this tree-widget-item: */
    270             return QSize(baseSizeHint.width(), iEffectiveHeightHint);
    271         }
    272         default:
    273             break;
    274     }
    275 
    276     /* Call to base-class: */
    277     return QTreeWidgetItem::data(iColumn, iRole);
    278 }
    279 
    280 QString UISnapshotItem::text(int iColumn) const
    281 {
    282     return QTreeWidgetItem::data(iColumn, Qt::DisplayRole).toString();
    283245}
    284246
     
    302264    /* Set/clear the bold font state
    303265     * for current snapshot item: */
    304     QFont myFont = font(0);
     266    QFont myFont = font(Column_Name);
    305267    myFont.setBold(fCurrent);
    306     setFont(0, myFont);
     268    setFont(Column_Name, myFont);
    307269
    308270    /* Update tool-tip: */
     
    321283                  ? UISnapshotPane::tr("Current State (changed)", "Current State (Modified)")
    322284                  : UISnapshotPane::tr("Current State", "Current State (Unmodified)");
    323         setText(0, m_strName);
     285        setText(Column_Name, m_strName);
    324286        m_strDescription = m_fCurrentStateModified
    325287                         ? UISnapshotPane::tr("The current state differs from the state stored in the current snapshot")
     
    335297        m_strSnapshotID = m_comSnapshot.GetId();
    336298        m_strName = m_comSnapshot.GetName();
    337         setText(0, m_strName);
     299        setText(Column_Name, m_strName);
    338300        m_fOnline = m_comSnapshot.GetOnline();
    339         setIcon(0, *m_pSnapshotWidget->snapshotItemIcon(m_fOnline));
     301        setIcon(Column_Name, *m_pSnapshotWidget->snapshotItemIcon(m_fOnline));
    340302        m_strDescription = m_comSnapshot.GetDescription();
    341303        m_timestamp.setTime_t(m_comSnapshot.GetTimeStamp() / 1000);
     
    343305    }
    344306
    345     /* Adjust text: */
    346     adjustText();
    347307    /* Update tool-tip: */
    348308    recacheToolTip();
     
    368328    m_enmMachineState = enmState;
    369329    /* Set corresponding icon: */
    370     setIcon(0, gpConverter->toIcon(m_enmMachineState));
     330    setIcon(Column_Name, gpConverter->toIcon(m_enmMachineState));
    371331    /* Update timestamp: */
    372332    m_timestamp.setTime_t(m_comMachine.GetLastStateChange() / 1000);
     
    386346    if (then.daysTo(now) > 30)
    387347    {
    388         strAge = UISnapshotPane::tr(" (%1)").arg(then.toString(Qt::LocalDate));
     348        strAge = then.toString(Qt::LocalDate);
    389349        enmAgeFormat = SnapshotAgeFormat_Max;
    390350    }
    391351    else if (then.secsTo(now) > 60 * 60 * 24)
    392352    {
    393         strAge = UISnapshotPane::tr(" (%1 ago)").arg(VBoxGlobal::daysToString(then.secsTo(now) / 60 / 60 / 24));
     353        strAge = UISnapshotPane::tr("%1 (%2 ago)", "date time (how long ago)")
     354                 .arg(then.toString(Qt::LocalDate), VBoxGlobal::daysToString(then.secsTo(now) / 60 / 60 / 24));
    394355        enmAgeFormat = SnapshotAgeFormat_InDays;
    395356    }
    396357    else if (then.secsTo(now) > 60 * 60)
    397358    {
    398         strAge = UISnapshotPane::tr(" (%1 ago)").arg(VBoxGlobal::hoursToString(then.secsTo(now) / 60 / 60));
     359        strAge = UISnapshotPane::tr("%1 (%2 ago)", "date time (how long ago)")
     360                 .arg(then.toString(Qt::LocalDate), VBoxGlobal::hoursToString(then.secsTo(now) / 60 / 60));
    399361        enmAgeFormat = SnapshotAgeFormat_InHours;
    400362    }
    401363    else if (then.secsTo(now) > 60)
    402364    {
    403         strAge = UISnapshotPane::tr(" (%1 ago)").arg(VBoxGlobal::minutesToString(then.secsTo(now) / 60));
     365        strAge = UISnapshotPane::tr("%1 (%2 ago)", "date time (how long ago)")
     366                 .arg(then.toString(Qt::LocalDate), VBoxGlobal::minutesToString(then.secsTo(now) / 60));
    404367        enmAgeFormat = SnapshotAgeFormat_InMinutes;
    405368    }
    406369    else
    407370    {
    408         strAge = UISnapshotPane::tr(" (%1 ago)").arg(VBoxGlobal::secondsToString(then.secsTo(now)));
     371        strAge = UISnapshotPane::tr("%1 (%2 ago)", "date time (how long ago)")
     372                 .arg(then.toString(Qt::LocalDate), VBoxGlobal::secondsToString(then.secsTo(now)));
    409373        enmAgeFormat = SnapshotAgeFormat_InSeconds;
    410374    }
    411375
    412376    /* Update data: */
    413     setData(0, Qt::UserRole, strAge);
     377    if (!m_fCurrentStateItem)
     378        setText(Column_Taken, strAge);
    414379
    415380    /* Return age: */
    416381    return enmAgeFormat;
    417 }
    418 
    419 void UISnapshotItem::adjustText()
    420 {
    421     /* Make sure item is initialised: */
    422     if (!treeWidget())
    423         return;
    424 
    425     /* Calculate metrics: */
    426     QFontMetrics metrics(font(0));
    427     int iHei0 = (metrics.height() > 16 ?
    428                  metrics.height() /* text */ : 16 /* icon */) +
    429                 2 * 2 /* 2 pixel per margin */;
    430     int iWid0 = metrics.width(text(0)) /* text */ +
    431                 treeWidget()->indentation() /* indent */ +
    432                 16 /* icon */;
    433 
    434     /* Adjust size finally: */
    435     setSizeHint(0, QSize(iWid0, iHei0));
    436382}
    437383
     
    458404    else
    459405    {
     406        /* Gather details: */
     407        QStringList details;
    460408        if (isCurrentSnapshotItem())
    461             strDetails = UISnapshotPane::tr(" (current, ", "Snapshot details");
    462         else
    463             strDetails = " (";
    464 
    465         /* Add online/offline information: */
    466         strDetails += m_fOnline ? UISnapshotPane::tr("online)", "Snapshot details")
    467                                 : UISnapshotPane::tr("offline)", "Snapshot details");
     409            details << UISnapshotPane::tr("current", "snapshot");
     410        details << (m_fOnline ? UISnapshotPane::tr("online", "snapshot")
     411                              : UISnapshotPane::tr("offline", "snapshot"));
     412        strDetails = QString(" (%1)").arg(details.join(", "));
    468413
    469414        /* Add date/time information: */
     
    476421    /* Prepare tool-tip: */
    477422    QString strToolTip = QString("<nobr><b>%1</b>%2</nobr><br><nobr>%3</nobr>")
    478                              .arg(text(0)).arg(strDetails).arg(strDateTime);
     423                             .arg(text(Column_Name)).arg(strDetails).arg(strDateTime);
    479424
    480425    /* Append description if any: */
     
    483428
    484429    /* Assign tool-tip finally: */
    485     setToolTip(0, strToolTip);
     430    setToolTip(Column_Name, strToolTip);
    486431}
    487432
     
    494439    : QITreeWidget(pParent)
    495440{
    496     /* No header: */
    497     header()->hide();
    498     /* All columns as one: */
     441    /* Configure snapshot tree: */
     442    setColumnCount(Column_Max);
    499443    setAllColumnsShowFocus(true);
    500     /* Our own context menu: */
     444    setAlternatingRowColors(true);
     445    setExpandsOnDoubleClick(false);
    501446    setContextMenuPolicy(Qt::CustomContextMenu);
     447    setEditTriggers(  QAbstractItemView::SelectedClicked
     448                    | QAbstractItemView::EditKeyPressed);
    502449}
    503450
     
    607554        m_pToolBar->updateLayout();
    608555#endif
     556
     557    /* Translate snapshot tree: */
     558    const QStringList fields = QStringList()
     559                               << tr("Name", "snapshot")
     560                               << tr("Taken", "snapshot");
     561    m_pSnapshotTree->setHeaderLabels(fields);
     562}
     563
     564void UISnapshotPane::resizeEvent(QResizeEvent *pEvent)
     565{
     566    /* Call to base-class: */
     567    QIWithRetranslateUI<QWidget>::resizeEvent(pEvent);
     568
     569    /* Adjust snapshot tree: */
     570    adjustTreeWidget();
     571}
     572
     573void UISnapshotPane::showEvent(QShowEvent *pEvent)
     574{
     575    /* Call to base-class: */
     576    QIWithRetranslateUI<QWidget>::showEvent(pEvent);
     577
     578    /* Adjust snapshot tree: */
     579    adjustTreeWidget();
    609580}
    610581
     
    734705    /* Allows editing again: */
    735706    m_pLockReadWrite->unlock();
     707
     708    /* Adjust snapshot tree: */
     709    adjustTreeWidget();
    736710}
    737711
     
    861835        {
    862836            /* Rename corresponding snapshot if necessary: */
    863             if (comSnapshot.GetName() != pSnapshotItem->text(0))
     837            if (comSnapshot.GetName() != pSnapshotItem->text(Column_Name))
    864838            {
    865839                /* We need to open a session when we manipulate the snapshot data of a machine: */
     
    870844
    871845                    /* Save snapshot name: */
    872                     comSnapshot.SetName(pSnapshotItem->text(0));
     846                    comSnapshot.SetName(pSnapshotItem->text(Column_Name));
    873847
    874848                    /* Close the session again: */
     
    881855    /* Allows editing again: */
    882856    m_pLockReadWrite->unlock();
     857
     858    /* Adjust snapshot tree: */
     859    adjustTreeWidget();
    883860}
    884861
     
    896873        {
    897874            /* As snapshot-restore procedure: */
    898             restoreSnapshot(true /* suppress non-critical warnings */);
     875            if (pSnapshotItem->snapshot().isNotNull())
     876                restoreSnapshot(true /* suppress non-critical warnings */);
    899877        }
    900878        /* Handle other kinds of DoubleClick: */
     
    956934        /* Prepare toolbar: */
    957935        prepareToolbar();
    958         /* Prepare tree-widget: */
     936        /* Prepare snapshot tree: */
    959937        prepareTreeWidget();
    960938        /* Prepare details-widget: */
     
    10551033    {
    10561034        /* Configure tree: */
    1057         m_pSnapshotTree->setExpandsOnDoubleClick(false);
    1058         m_pSnapshotTree->setEditTriggers(  QAbstractItemView::SelectedClicked
    1059                                          | QAbstractItemView::EditKeyPressed);
    10601035        connect(m_pSnapshotTree, &UISnapshotTree::currentItemChanged,
    10611036                this, &UISnapshotPane::sltHandleCurrentItemChange);
     
    11591134
    11601135    /* Adjust snapshot tree: */
    1161     m_pSnapshotTree->resizeColumnToContents(0);
     1136    adjustTreeWidget();
    11621137}
    11631138
     
    12471222            while (*iterator)
    12481223            {
    1249                 QString strSnapshot = static_cast<UISnapshotItem*>(*iterator)->text(0);
     1224                QString strSnapshot = static_cast<UISnapshotItem*>(*iterator)->text(Column_Name);
    12501225                int iPos = regExp.indexIn(strSnapshot);
    12511226                if (iPos != -1)
     
    12981273    }
    12991274    while (0);
     1275
     1276    /* Adjust snapshot tree: */
     1277    adjustTreeWidget();
    13001278
    13011279    /* Return result: */
     
    13731351    while (0);
    13741352
     1353    /* Adjust snapshot tree: */
     1354    adjustTreeWidget();
     1355
    13751356    /* Return result: */
    13761357    return fSuccess;
     
    14481429    while (0);
    14491430
     1431    /* Adjust snapshot tree: */
     1432    adjustTreeWidget();
     1433
    14501434    /* Return result: */
    14511435    return fSuccess;
     
    14791463}
    14801464
     1465void UISnapshotPane::adjustTreeWidget()
     1466{
     1467    /* Get the snapshot tree abstract interface: */
     1468    QAbstractItemView *pItemView = m_pSnapshotTree;
     1469    /* Get the snapshot tree header-view: */
     1470    QHeaderView *pItemHeader = m_pSnapshotTree->header();
     1471
     1472    /* Calculate the total snapshot tree width: */
     1473    const int iTotal = m_pSnapshotTree->viewport()->width();
     1474    /* Look for a minimum width hints for non-important columns: */
     1475    const int iMinWidth1 = qMax(pItemView->sizeHintForColumn(Column_Taken), pItemHeader->sectionSizeHint(Column_Taken));
     1476    /* Propose suitable width hints for non-important columns: */
     1477    const int iWidth1 = iMinWidth1 < iTotal / Column_Max ? iMinWidth1 : iTotal / Column_Max;
     1478    /* Apply the proposal: */
     1479    m_pSnapshotTree->setColumnWidth(Column_Taken, iWidth1);
     1480    m_pSnapshotTree->setColumnWidth(Column_Name, iTotal - iWidth1);
     1481}
     1482
    14811483UISnapshotItem *UISnapshotPane::findItem(const QString &strSnapshotID) const
    14821484{
  • trunk/src/VBox/Frontends/VirtualBox/src/selector/UISnapshotPane.h

    r67260 r67262  
    7373        /** Handles translation event. */
    7474        virtual void retranslateUi() /* override */;
     75
     76        /** Handles resize @a pEvent. */
     77        virtual void resizeEvent(QResizeEvent *pEvent) /* override */;
     78
     79        /** Handles show @a pEvent. */
     80        virtual void showEvent(QShowEvent *pEvent) /* override */;
    7581    /** @} */
    7682
     
    159165    /** @name Tree-widget helpers.
    160166      * @{ */
     167        /** Handles command to adjust snapshot tree. */
     168        void adjustTreeWidget();
     169
    161170        /** Searches for an item with corresponding @a strSnapshotID. */
    162171        UISnapshotItem *findItem(const QString &strSnapshotID) const;
Note: See TracChangeset for help on using the changeset viewer.

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