VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxSharedFoldersSettings.ui.h@ 953

Last change on this file since 953 was 952, checked in by vboxsync, 18 years ago

FE/Qt: Spelling.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * "Shared Folders" settings dialog UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23/****************************************************************************
24** ui.h extension file, included from the uic-generated form implementation.
25**
26** If you want to add, delete, or rename functions or slots, use
27** Qt Designer to update this file, preserving your code.
28**
29** You should not define a constructor or destructor in this file.
30** Instead, write your code in functions called init() and destroy().
31** These will automatically be called by the form's constructor and
32** destructor.
33*****************************************************************************/
34
35
36class VBoxRichListItem : public QListViewItem
37{
38public:
39
40 enum { QIRichListItemId = 1010 };
41
42 enum FormatType
43 {
44 IncorrectFormat = 0,
45 EllipsisStart = 1,
46 EllipsisMiddle = 2,
47 EllipsisEnd = 3,
48 EllipsisFile = 4
49 };
50
51 VBoxRichListItem (FormatType aFormat, QListView *aParent,
52 const QString& aName, const QString& aNull,
53 const QString& aKey) :
54 QListViewItem (aParent, aName, aNull, aKey), mFormat (aFormat)
55 {
56 }
57
58 VBoxRichListItem (FormatType aFormat, QListViewItem *aParent,
59 const QString& aName, const QString& aPath) :
60 QListViewItem (aParent, aName, aPath), mFormat (aFormat)
61 {
62 mTextList << aName << aPath;
63 }
64
65 int rtti() const { return QIRichListItemId; }
66
67 const QString& getText (int aIndex) { return mTextList [aIndex]; }
68
69protected:
70
71 void paintCell (QPainter *aPainter, const QColorGroup &aColorGroup,
72 int aColumn, int aWidth, int aAlign)
73 {
74 processColumn (aColumn, aWidth);
75 QListViewItem::paintCell (aPainter, aColorGroup, aColumn, aWidth, aAlign);
76 }
77
78 void processColumn (int aColumn, int aWidth)
79 {
80 QString oneString = mTextList [aColumn];
81 if (oneString.isEmpty())
82 return;
83 int oldSize = listView()->fontMetrics().width (oneString);
84 int indentSize = listView()->fontMetrics().width ("...x");
85
86 /* compress text */
87 int start = 0;
88 int finish = 0;
89 int position = 0;
90 int textWidth = 0;
91 do {
92 textWidth = listView()->fontMetrics().width (oneString);
93 if (textWidth + indentSize > aWidth)
94 {
95 start = 0;
96 finish = oneString.length();
97
98 /* selecting remove position */
99 switch (mFormat)
100 {
101 case EllipsisStart:
102 position = start;
103 break;
104 case EllipsisMiddle:
105 position = (finish - start) / 2;
106 break;
107 case EllipsisEnd:
108 position = finish - 1;
109 break;
110 case EllipsisFile:
111 {
112 QRegExp regExp ("([\\\\/][^\\\\^/]+[\\\\/]?$)");
113 int newFinish = regExp.search (oneString);
114 if (newFinish != -1)
115 finish = newFinish;
116 position = (finish - start) / 2;
117 break;
118 }
119 default:
120 AssertMsgFailed (("Invalid format type\n"));
121 }
122
123 if (position == finish)
124 break;
125 oneString.remove (position, 1);
126 }
127 } while (textWidth + indentSize > aWidth);
128 if (position || mFormat == EllipsisFile) oneString.insert (position, "...");
129
130 int newSize = listView()->fontMetrics().width (oneString);
131 setText (aColumn, newSize < oldSize ? oneString : mTextList [aColumn]);
132 }
133
134 FormatType mFormat;
135 QStringList mTextList;
136};
137
138
139class VBoxAddSFDialog : public QDialog
140{
141 Q_OBJECT
142
143public:
144
145 VBoxAddSFDialog (QWidget *aParent) :
146 QDialog (aParent, "VBoxAddSFDialog", true /* modal */),
147 mLePath (0), mLeName (0)
148 {
149 QVBoxLayout *mainLayout = new QVBoxLayout (this, 10, 10, "mainLayout");
150
151 /* Setup Input layout */
152 QGridLayout *inputLayout = new QGridLayout (mainLayout, 2, 3, 10, "inputLayout");
153 QLabel *lbPath = new QLabel ("Folder Path", this);
154 mLePath = new QLineEdit (this);
155 QToolButton *tbPath = new QToolButton (this);
156 QLabel *lbName = new QLabel ("Folder Name", this);
157 mLeName = new QLineEdit (this);
158 tbPath->setIconSet (VBoxGlobal::iconSet ("select_file_16px.png",
159 "select_file_dis_16px.png"));
160 tbPath->setFocusPolicy (QWidget::TabFocus);
161 connect (mLePath, SIGNAL (textChanged (const QString &)),
162 this, SLOT (validate()));
163 connect (mLeName, SIGNAL (textChanged (const QString &)),
164 this, SLOT (validate()));
165 connect (tbPath, SIGNAL (clicked()), this, SLOT (showFileDialog()));
166 QWhatsThis::add (mLePath, tr ("Enter existing path for the shared folder here"));
167 QWhatsThis::add (mLeName, tr ("Enter name for the shared folder to be created"));
168 QWhatsThis::add (tbPath, tr ("Click to invoke <open folder> dialog"));
169
170 inputLayout->addWidget (lbPath, 0, 0);
171 inputLayout->addWidget (mLePath, 0, 1);
172 inputLayout->addWidget (tbPath, 0, 2);
173 inputLayout->addWidget (lbName, 1, 0);
174 inputLayout->addMultiCellWidget (mLeName, 1, 1, 1, 2);
175
176 /* Setup Button layout */
177 QHBoxLayout *buttonLayout = new QHBoxLayout (mainLayout, 10, "buttonLayout");
178 mBtOk = new QPushButton ("OK", this, "btOk");
179 QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
180 QPushButton *btCancel = new QPushButton ("Cancel", this, "btCancel");
181 connect (mBtOk, SIGNAL (clicked()), this, SLOT (accept()));
182 connect (btCancel, SIGNAL (clicked()), this, SLOT (reject()));
183
184 buttonLayout->addWidget (mBtOk);
185 buttonLayout->addItem (spacer);
186 buttonLayout->addWidget (btCancel);
187
188 /* Validate fields */
189 validate();
190 }
191
192 ~VBoxAddSFDialog() {}
193
194 QString getPath() { return mLePath->text(); }
195
196 QString getName() { return mLeName->text(); }
197
198private slots:
199
200 void validate()
201 {
202 mBtOk->setEnabled (!mLePath->text().isEmpty() && !mLeName->text().isEmpty());
203 }
204
205 void showFileDialog()
206 {
207 QFileDialog dlg (QDir::rootDirPath(), QString::null, this);
208 dlg.setMode (QFileDialog::DirectoryOnly);
209 dlg.setCaption (tr ("Add Share"));
210 if (dlg.exec() == QDialog::Accepted)
211 {
212 QString folderName = QDir::convertSeparators (dlg.selectedFile());
213 QRegExp commonRule ("[\\\\/]([^\\\\^/]+)[\\\\/]?$");
214 QRegExp rootRule ("(([a-zA-Z])[^\\\\^/])?[\\\\/]$");
215 if (commonRule.search (folderName) != -1)
216 {
217 /* processing non-root folder */
218 mLePath->setText (folderName.remove (QRegExp ("[\\\\/]$")));
219 mLeName->setText (commonRule.cap (1));
220 }
221 else if (rootRule.search (folderName) != -1)
222 {
223 /* processing root folder */
224 mLePath->setText (folderName);
225#if defined(Q_WS_WIN32)
226 mLeName->setText (rootRule.cap (2) + "_DRIVE");
227#elif defined(Q_WS_X11)
228 mLeName->setText ("ROOT");
229#endif
230 }
231 else
232 return; /* hm, what type of folder it was? */
233 }
234 }
235
236private:
237
238 void showEvent (QShowEvent *aEvent)
239 {
240 setFixedHeight (height());
241 QDialog::showEvent (aEvent);
242 }
243
244 QPushButton *mBtOk;
245 QLineEdit *mLePath;
246 QLineEdit *mLeName;
247};
248
249
250void VBoxSharedFoldersSettings::init()
251{
252 mDialogType = WrongType;
253 new QIListViewSelectionPreserver (this, listView);
254 listView->setShowToolTips (false);
255 tbAdd->setIconSet (VBoxGlobal::iconSet ("select_file_16px.png",
256 "select_file_dis_16px.png"));
257 tbRemove->setIconSet (VBoxGlobal::iconSet ("eraser_16px.png",
258 "eraser_disabled_16px.png"));
259 connect (tbAdd, SIGNAL (clicked()), this, SLOT (tbAddPressed()));
260 connect (tbRemove, SIGNAL (clicked()), this, SLOT (tbRemovePressed()));
261 connect (listView, SIGNAL (currentChanged (QListViewItem *)),
262 this, SLOT (processCurrentChanged (QListViewItem *)));
263 connect (listView, SIGNAL (onItem (QListViewItem *)),
264 this, SLOT (processOnItem (QListViewItem *)));
265
266 mIsListViewChanged = false;
267}
268
269void VBoxSharedFoldersSettings::setDialogType (
270 VBoxSharedFoldersSettings::SFDialogType aType)
271{
272 mDialogType = aType;
273}
274
275
276void VBoxSharedFoldersSettings::removeSharedFolder (const QString & aName,
277 const QString & aPath,
278 SFDialogType aType)
279{
280 switch (aType)
281 {
282 case GlobalType:
283 {
284 /* This feature is not implemented yet */
285 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
286 break;
287 }
288 case MachineType:
289 {
290 Assert (!mMachine.isNull());
291 mMachine.RemoveSharedFolder (aName);
292 if (!mMachine.isOk())
293 vboxProblem().cannotRemoveSharedFolder (this, mMachine,
294 aName, aPath);
295 break;
296 }
297 case ConsoleType:
298 {
299 Assert (!mConsole.isNull());
300 mConsole.RemoveSharedFolder (aName);
301 if (!mConsole.isOk())
302 vboxProblem().cannotRemoveSharedFolder (this, mConsole.GetMachine(),
303 aName, aPath);
304 break;
305 }
306 default:
307 {
308 AssertMsgFailed (("Incorrect shared folder type\n"));
309 }
310 }
311}
312
313void VBoxSharedFoldersSettings::createSharedFolder (const QString & aName,
314 const QString & aPath,
315 SFDialogType aType)
316{
317 switch (aType)
318 {
319 case GlobalType:
320 {
321 /* This feature is not implemented yet */
322 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
323 break;
324 }
325 case MachineType:
326 {
327 Assert (!mMachine.isNull());
328 mMachine.CreateSharedFolder (aName, aPath);
329 if (!mMachine.isOk())
330 vboxProblem().cannotCreateSharedFolder (this, mMachine,
331 aName, aPath);
332 break;
333 }
334 case ConsoleType:
335 {
336 Assert (!mConsole.isNull());
337 mConsole.CreateSharedFolder (aName, aPath);
338 if (!mConsole.isOk())
339 vboxProblem().cannotCreateSharedFolder (this, mConsole.GetMachine(),
340 aName, aPath);
341 break;
342 }
343 default:
344 {
345 AssertMsgFailed (("Incorrect shared folder type\n"));
346 }
347 }
348}
349
350
351void VBoxSharedFoldersSettings::getFromGlobal()
352{
353 /* This feature is not implemented yet */
354 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
355
356 /*
357 QString name = tr (" Global Folders");
358 QString key (QString::number (GlobalType));
359 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
360 listView, name, QString::null, key);
361 getFrom (vboxGlobal().virtualBox().GetSharedFolders().Enumerate(), root);
362 */
363}
364
365void VBoxSharedFoldersSettings::getFromMachine (const CMachine &aMachine)
366{
367 mMachine = aMachine;
368 QString name = tr (" Machine Folders");
369 QString key (QString::number (MachineType));
370 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
371 listView, name, QString::null, key);
372 getFrom (mMachine.GetSharedFolders().Enumerate(), root);
373}
374
375void VBoxSharedFoldersSettings::getFromConsole (const CConsole &aConsole)
376{
377 mConsole = aConsole;
378 QString name = tr (" Transient Folders");
379 QString key (QString::number (ConsoleType));
380 VBoxRichListItem *root = new VBoxRichListItem (VBoxRichListItem::EllipsisEnd,
381 listView, name, QString::null, key);
382 getFrom (mConsole.GetSharedFolders().Enumerate(), root);
383}
384
385void VBoxSharedFoldersSettings::getFrom (const CSharedFolderEnumerator &aEn,
386 QListViewItem *aRoot)
387{
388 aRoot->setSelectable (false);
389 while (aEn.HasMore())
390 {
391 CSharedFolder sf = aEn.GetNext();
392 new VBoxRichListItem (VBoxRichListItem::EllipsisFile, aRoot,
393 sf.GetName(), sf.GetHostPath());
394 }
395 listView->setOpen (aRoot, true);
396 listView->setCurrentItem (aRoot);
397 processCurrentChanged (aRoot);
398}
399
400
401void VBoxSharedFoldersSettings::putBackToGlobal()
402{
403 /* This feature is not implemented yet */
404 AssertMsgFailed (("Global shared folders are not implemented yet\n"));
405
406 /*
407 if (!mIsListViewChanged) return;
408 // This function is only available for GlobalType dialog
409 Assert (mDialogType == GlobalType);
410 // Searching for GlobalType item's root
411 QListViewItem *root = listView->findItem (QString::number (GlobalType), 2);
412 Assert (root);
413 CSharedFolderEnumerator en = vboxGlobal().virtualBox().GetSharedFolders().Enumerate();
414 putBackTo (en, root);
415 */
416}
417
418void VBoxSharedFoldersSettings::putBackToMachine()
419{
420 if (!mIsListViewChanged)
421 return;
422
423 /* This function is only available for MachineType dialog */
424 Assert (mDialogType == MachineType);
425 /* Searching for MachineType item's root */
426 QListViewItem *root = listView->findItem (QString::number (MachineType), 2);
427 Assert (root);
428 CSharedFolderEnumerator en = mMachine.GetSharedFolders().Enumerate();
429 putBackTo (en, root);
430}
431
432void VBoxSharedFoldersSettings::putBackToConsole()
433{
434 if (!mIsListViewChanged)
435 return;
436
437 /* This function is only available for ConsoleType dialog */
438 Assert (mDialogType == ConsoleType);
439 /* Searching for ConsoleType item's root */
440 QListViewItem *root = listView->findItem (QString::number (ConsoleType), 2);
441 Assert (root);
442 CSharedFolderEnumerator en = mConsole.GetSharedFolders().Enumerate();
443 putBackTo (en, root);
444}
445
446void VBoxSharedFoldersSettings::putBackTo (CSharedFolderEnumerator &aEn,
447 QListViewItem *aRoot)
448{
449 SFDialogType type = (SFDialogType)aRoot->text (2).toInt();
450 Assert (type);
451
452 /* deleting all existing folders if the list */
453 while (aEn.HasMore())
454 {
455 CSharedFolder sf = aEn.GetNext();
456 removeSharedFolder (sf.GetName(), sf.GetHostPath(), type);
457 }
458
459 /* saving all machine related list view items */
460 QListViewItem *iterator = aRoot->firstChild();
461 while (iterator)
462 {
463 VBoxRichListItem *item = 0;
464 if (iterator->rtti() == VBoxRichListItem::QIRichListItemId)
465 item = static_cast<VBoxRichListItem*> (iterator);
466 if (item)
467 createSharedFolder (item->getText (0), item->getText (1), type);
468 else
469 AssertMsgFailed (("Incorrect listview item type\n"));
470 iterator = iterator->nextSibling();
471 }
472}
473
474
475void VBoxSharedFoldersSettings::tbAddPressed()
476{
477 /* Invoke Add-Box Dialog */
478 VBoxAddSFDialog dlg (this);
479 if (dlg.exec() != QDialog::Accepted)
480 return;
481 QString name = dlg.getName();
482 QString path = dlg.getPath();
483 /* Shared folder's name & path could not be empty */
484 Assert (!name.isEmpty() && !path.isEmpty());
485 /* Searching root for the new listview item */
486 QListViewItem *root = listView->findItem (QString::number (mDialogType), 2);
487 Assert (root);
488 /* Appending a new listview item to the root */
489 VBoxRichListItem *item = new VBoxRichListItem (VBoxRichListItem::EllipsisFile,
490 root, name, path);
491 listView->ensureItemVisible (item);
492 listView->setCurrentItem (item);
493 processCurrentChanged (item);
494 listView->setFocus();
495 mIsListViewChanged = true;
496}
497
498void VBoxSharedFoldersSettings::tbRemovePressed()
499{
500 Assert (listView->selectedItem());
501 delete listView->selectedItem();
502 mIsListViewChanged = true;
503}
504
505
506void VBoxSharedFoldersSettings::processOnItem (QListViewItem *aItem)
507{
508 VBoxRichListItem *item = 0;
509 if (aItem->rtti() == VBoxRichListItem::QIRichListItemId)
510 item = static_cast<VBoxRichListItem*> (aItem);
511 Assert (item);
512 QString tip = tr ("<nobr>Name:&nbsp;&nbsp;%1</nobr><br>"
513 "<nobr>Path:&nbsp;&nbsp;%2</nobr>")
514 .arg (item->getText (0)).arg (item->getText (1));
515 if (!item->getText (0).isEmpty() && !item->getText (1).isEmpty())
516 QToolTip::add (listView->viewport(), listView->itemRect (aItem), tip);
517 else
518 QToolTip::remove (listView->viewport());
519}
520
521void VBoxSharedFoldersSettings::processCurrentChanged (QListViewItem *aItem)
522{
523 if (aItem && listView->selectedItem() != aItem)
524 listView->setSelected (aItem, true);
525 bool removeEnabled = aItem && aItem->parent() &&
526 isEditable (aItem->parent()->text (2));
527 bool addEnabled = aItem &&
528 (isEditable (aItem->text (2)) ||
529 aItem->parent() && isEditable (aItem->parent()->text (2)));
530 tbRemove->setEnabled (removeEnabled);
531 tbAdd->setEnabled (addEnabled);
532}
533
534bool VBoxSharedFoldersSettings::isEditable (const QString &aKey)
535{
536 /* mDialogType should be sutup already */
537 Assert (mDialogType);
538 /* simple item has no key information */
539 if (aKey.isEmpty())
540 return false;
541 SFDialogType type = (SFDialogType)aKey.toInt();
542 if (!type)
543 AssertMsgFailed (("Incorrect listview item key value\n"));
544 return type == mDialogType;
545}
546
547
548#include "VBoxSharedFoldersSettings.ui.moc"
549
Note: See TracBrowser for help on using the repository browser.

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