VirtualBox

Changeset 71269 in vbox


Ignore:
Timestamp:
Mar 8, 2018 10:50:12 AM (7 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:6699 Implementing GUI for 'create new directory' in guest control file manager

Location:
trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl/UIGuestControlFileTable.cpp

    r71258 r71269  
    2727# include <QItemDelegate>
    2828# include <QGridLayout>
     29# include <QPushButton>
    2930
    3031/* GUI includes: */
     32# include "QIDialog.h"
     33# include "QIDialogButtonBox.h"
    3134# include "QILabel.h"
    3235# include "QILineEdit.h"
     
    4447#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    4548
     49
     50/*********************************************************************************************************************************
     51*   UIFileDelegate definition.                                                                                                   *
     52*********************************************************************************************************************************/
     53/** A QItemDelegate child class to disable dashed lines drawn around selected cells in QTableViews */
    4654class UIFileDelegate : public QItemDelegate
    4755{
     
    5260        virtual void drawFocus ( QPainter * /*painter*/, const QStyleOptionViewItem & /*option*/, const QRect & /*rect*/ ) const {}
    5361};
     62
     63
     64/*********************************************************************************************************************************
     65*   UIFileStringInputDialog definition.                                                                                          *
     66*********************************************************************************************************************************/
     67/** A QIDialog child including a line edit whose text exposed when the dialog is accepted */
     68class UIStringInputDialog : public QIDialog
     69{
     70
     71    Q_OBJECT;
     72
     73public:
     74
     75    UIStringInputDialog(QWidget *pParent = 0, Qt::WindowFlags flags = 0);
     76    QString getString() const;
     77
     78private:
     79
     80    QILineEdit      *m_pLineEdit;
     81
     82    // virtual void accept() /* override */;
     83    // virtual void reject() /* override */;
     84
     85};
     86
    5487
    5588
     
    106139
    107140
     141/*********************************************************************************************************************************
     142*   UIFileStringInputDialog implementation.                                                                                      *
     143*********************************************************************************************************************************/
     144UIStringInputDialog::UIStringInputDialog(QWidget *pParent /* = 0 */, Qt::WindowFlags flags /* = 0 */)
     145    :QIDialog(pParent, flags)
     146{
     147    QVBoxLayout *layout = new QVBoxLayout(this);
     148    m_pLineEdit = new QILineEdit(this);
     149    layout->addWidget(m_pLineEdit);
     150
     151    QIDialogButtonBox *pButtonBox =
     152        new QIDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
     153    layout->addWidget(pButtonBox);
     154        // {
     155        //     /* Configure button-box: */
     156    connect(pButtonBox, &QIDialogButtonBox::accepted, this, &UIStringInputDialog::accept);
     157    connect(pButtonBox, &QIDialogButtonBox::rejected, this, &UIStringInputDialog::reject);
     158
     159}
     160
     161QString UIStringInputDialog::getString() const
     162{
     163    if (!m_pLineEdit)
     164        return QString();
     165    return m_pLineEdit->text();
     166}
     167
     168
     169/*********************************************************************************************************************************
     170*   UIFileTableItem implementation.                                                                                              *
     171*********************************************************************************************************************************/
     172
     173
    108174UIFileTableItem::UIFileTableItem(const QList<QVariant> &data, bool isDirectory, UIFileTableItem *parent)
    109175    : m_itemData(data)
     
    306372        if (item->isUpDirectory() && index.column() != 0)
    307373            return QVariant();
     374        /* Format date/time column: */
     375        if (item->data(index.column()).canConvert(QMetaType::QDateTime))
     376        {
     377            QDateTime dateTime = item->data(index.column()).toDateTime();
     378            if (dateTime.isValid())
     379                return dateTime.toString("dd.MM.yyyy hh:mm:ss");
     380        }
    308381        return item->data(index.column());
    309382    }
     
    430503{
    431504    endResetModel();
     505}
     506
     507bool UIGuestControlFileModel::insertRows(int position, int rows, const QModelIndex &parent)
     508{
     509    UIFileTableItem *parentItem = static_cast<UIFileTableItem*>(parent.internalPointer());
     510
     511    if (!parentItem)
     512        return false;
     513    beginInsertRows(parent, position, position + rows -1);
     514
     515    QList<QVariant> data;
     516    data << "New Item" << 0 << QDateTime::currentDateTime();
     517    UIFileTableItem *newItem = new UIFileTableItem(data, true, parentItem);
     518    parentItem->appendChild(newItem);
     519    endInsertRows();
     520
     521    return true;
    432522}
    433523
     
    452542    , m_pDelete(0)
    453543    , m_pRename(0)
    454     , m_pNewFolder(0)
     544    , m_pCreateNewDirectory(0)
    455545    , m_pCopy(0)
    456546    , m_pCut(0)
     
    586676    }
    587677
    588     m_pNewFolder = new QAction(this);
    589     if (m_pNewFolder)
    590     {
    591         m_pNewFolder->setIcon(UIIconPool::iconSet(QString(":/sf_32px.png")));
    592         m_pToolBar->addAction(m_pNewFolder);
    593         m_pNewFolder->setEnabled(false);
     678    m_pCreateNewDirectory = new QAction(this);
     679    if (m_pCreateNewDirectory)
     680    {
     681        connect(m_pCreateNewDirectory, &QAction::triggered, this, &UIGuestControlFileTable::sltCreateNewDirectory);
     682        m_pCreateNewDirectory->setIcon(UIIconPool::iconSet(QString(":/sf_32px.png")));
     683        m_pToolBar->addAction(m_pCreateNewDirectory);
    594684    }
    595685
     
    812902        deleteByIndex(selectedItemIndices.at(i));
    813903    }
     904    /** @todo dont refresh here, just delete the rows and update the table view: */
    814905    refresh();
    815906}
     
    830921        return;
    831922    m_pView->edit(selectedItemIndices.at(0));
    832 
    833 }
     923}
     924
     925void UIGuestControlFileTable::sltCreateNewDirectory()
     926{
     927    if (!m_pModel || !m_pView)
     928        return;
     929    QModelIndex currentIndex = m_pView->rootIndex();
     930    if (!currentIndex.isValid())
     931        return;
     932    UIFileTableItem *item = static_cast<UIFileTableItem*>(currentIndex.internalPointer());
     933    if (!item)
     934        return;
     935
     936    QString newDirectoryName = getNewDirectoryName();
     937    if (newDirectoryName.isEmpty())
     938        return;
     939
     940    if (createDirectory(item->path(), newDirectoryName))
     941    {
     942        /** @todo instead of refreshing here (an overkill) just add the
     943           rows and update the tabel view: */
     944        sltRefresh();
     945    }
     946
     947}
     948
    834949
    835950void UIGuestControlFileTable::deleteByIndex(const QModelIndex &itemIndex)
     
    852967    if (m_pGoHome)
    853968    {
    854         m_pGoHome->setText(UIVMInformationDialog::tr("Go to home folder"));
    855         m_pGoHome->setToolTip(UIVMInformationDialog::tr("Go to home folder"));
    856         m_pGoHome->setStatusTip(UIVMInformationDialog::tr("Go to home folder"));
     969        m_pGoHome->setText(UIVMInformationDialog::tr("Go to home directory"));
     970        m_pGoHome->setToolTip(UIVMInformationDialog::tr("Go to home directory"));
     971        m_pGoHome->setStatusTip(UIVMInformationDialog::tr("Go to home directory"));
    857972    }
    858973
     
    877992    }
    878993
    879     if (m_pNewFolder)
    880     {
    881         m_pNewFolder->setText(UIVMInformationDialog::tr("Create a new folder"));
    882         m_pNewFolder->setToolTip(UIVMInformationDialog::tr("Create a new folder"));
    883         m_pNewFolder->setStatusTip(UIVMInformationDialog::tr("Create a new folder"));
     994    if (m_pCreateNewDirectory)
     995    {
     996        m_pCreateNewDirectory->setText(UIVMInformationDialog::tr("Create a new directory"));
     997        m_pCreateNewDirectory->setToolTip(UIVMInformationDialog::tr("Create a new directory"));
     998        m_pCreateNewDirectory->setStatusTip(UIVMInformationDialog::tr("Create a new directory"));
    884999
    8851000    }
     
    9271042        }
    9281043    }
    929     else if(pEvent->key() == Qt::Key_Delete)
     1044    else if (pEvent->key() == Qt::Key_Delete)
    9301045    {
    9311046        sltDelete();
     
    9581073}
    9591074
     1075QString UIGuestControlFileTable::mergePaths(const QString &path, const QString &baseName)
     1076{
     1077    QString newPath(path);
     1078    /* make sure we have a trailing '/': */
     1079    if (newPath.at(newPath.length() - 1) != QChar('/'))
     1080        newPath += QChar('/');
     1081    newPath += baseName;
     1082    return newPath;
     1083}
     1084
     1085QString UIGuestControlFileTable::getNewDirectoryName()
     1086{
     1087    UIStringInputDialog *dialog = new UIStringInputDialog();
     1088    if (dialog->execute())
     1089    {
     1090        QString strDialog = dialog->getString();
     1091        delete dialog;
     1092        return strDialog;
     1093    }
     1094    delete dialog;
     1095    return QString();
     1096}
     1097
    9601098
    9611099/*********************************************************************************************************************************
     
    10121150        {
    10131151            QList<QVariant> data;
    1014             QDateTime changeTime = QDateTime::fromMSecsSinceEpoch(fsInfo.GetChangeTime()/1000);
     1152            QDateTime changeTime = QDateTime::fromMSecsSinceEpoch(fsInfo.GetChangeTime()/1000000);
     1153
    10151154            data << fsInfo.GetName() << static_cast<qulonglong>(fsInfo.GetObjectSize()) << changeTime;
    10161155            bool isDirectory = (fsInfo.GetType() == KFsObjType_Directory);
     
    10811220}
    10821221
     1222bool UIGuestFileTable::createDirectory(const QString &path, const QString &directoryName)
     1223{
     1224    if (!m_comGuestSession.isOk())
     1225        return false;
     1226
     1227    QString newDirectoryPath = mergePaths(path, directoryName);
     1228    QVector<KDirectoryCreateFlag> flags(KDirectoryCreateFlag_None);
     1229
     1230    m_comGuestSession.DirectoryCreate(newDirectoryPath, 777/*aMode*/, flags);
     1231    if (!m_comGuestSession.isOk())
     1232    {
     1233        emit sigLogOutput(newDirectoryPath.append(" could not be created"));
     1234        return false;
     1235    }
     1236    emit sigLogOutput(newDirectoryPath.append(" has been created"));
     1237    return true;
     1238}
     1239
    10831240
    10841241/*********************************************************************************************************************************
     
    11601317void UIHostFileTable::goToHomeDirectory()
    11611318{
    1162     if(!m_pRootItem || m_pRootItem->childCount() <= 0)
     1319    if (!m_pRootItem || m_pRootItem->childCount() <= 0)
    11631320        return;
    11641321    UIFileTableItem *startDirItem = m_pRootItem->child(0);
     
    11871344}
    11881345
     1346bool UIHostFileTable::createDirectory(const QString &path, const QString &directoryName)
     1347{
     1348    QDir parentDir(path);
     1349    if (!parentDir.mkdir(directoryName))
     1350    {
     1351        emit sigLogOutput(mergePaths(path, directoryName).append(" could not be created"));
     1352        return false;
     1353    }
     1354
     1355    return true;
     1356}
    11891357
    11901358#include "UIGuestControlFileTable.moc"
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl/UIGuestControlFileTable.h

    r71258 r71269  
    7272    void beginReset();
    7373    void endReset();
     74    bool insertRows(int position, int rows, const QModelIndex &parent);
    7475
    7576private:
     
    115116    virtual void goToHomeDirectory() = 0;
    116117    virtual bool renameItem(UIFileTableItem *item, QString newBaseName) = 0;
     118    virtual bool createDirectory(const QString &path, const QString &directoryName) = 0;
    117119    void             goIntoDirectory(const QModelIndex &itemIndex);
    118120    /** Follow the path trail, open directories as we go and descend */
     
    125127    /** Replace the last part of the @p previusPath with newBaseName */
    126128    QString constructNewItemPath(const QString &previousPath, const QString &newBaseName);
     129    QString mergePaths(const QString &Path, const QString &baseName);
    127130
    128131    UIFileTableItem         *m_pRootItem;
     
    144147    void sltDelete();
    145148    void sltRename();
     149    void sltCreateNewDirectory();
    146150
    147151private:
     
    152156    /** Return the UIFileTableItem for path / which is a direct (and single) child of m_pRootItem */
    153157    UIFileTableItem *getStartDirectoryItem();
    154 
     158    /** Shows a modal dialog with a line edit for user to enter a new directory name and return the entered string*/
     159    QString         getNewDirectoryName();
    155160    QGridLayout     *m_pMainLayout;
    156161    QILineEdit      *m_pCurrentLocationEdit;
     
    161166    QAction         *m_pDelete;
    162167    QAction         *m_pRename;
    163     QAction         *m_pNewFolder;
     168    QAction         *m_pCreateNewDirectory;
    164169
    165170    QAction         *m_pCopy;
     
    188193    virtual void goToHomeDirectory() /* override */;
    189194    virtual bool renameItem(UIFileTableItem *item, QString newBaseName);
     195    virtual bool createDirectory(const QString &path, const QString &directoryName);
    190196
    191197private:
     
    213219    virtual void goToHomeDirectory() /* override */;
    214220    virtual bool renameItem(UIFileTableItem *item, QString newBaseName);
     221    virtual bool createDirectory(const QString &path, const QString &directoryName);
    215222};
    216223
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