Changeset 4364 in vbox for trunk/src/VBox
- Timestamp:
- Aug 24, 2007 6:34:50 PM (17 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/include/QIWidgetValidator.h
r4326 r4364 24 24 #include <qobject.h> 25 25 #include <qvalidator.h> 26 #include <qvaluelist.h> 26 27 27 28 class QIWidgetValidator : public QObject … … 30 31 31 32 public: 32 QIWidgetValidator( QWidget *widget, QObject *parent = 0, const char *name = 0 ); 33 34 QIWidgetValidator (QWidget *aWidget, QObject *aParent = 0, 35 const char *aName = 0); 36 QIWidgetValidator (const QString &aCaption, 37 QWidget *aWidget, QObject *aParent = 0, 38 const char *aName = 0); 33 39 ~QIWidgetValidator(); 34 40 35 QWidget *widget() const { return wgt; }41 QWidget *widget() const { return mWidget; } 36 42 bool isValid() const; 37 43 void rescan(); 38 44 39 void setOtherValid( bool valid ) { otherValid = valid; } 40 bool isOtherValid() const { return otherValid; } 45 QString warningText() const; 46 47 void setOtherValid (bool aValid) { mOtherValid = aValid; } 48 bool isOtherValid() const { return mOtherValid; } 41 49 42 50 signals: 43 void validityChanged( const QIWidgetValidator *wval ); 44 void isValidRequested( QIWidgetValidator *wval ); 51 52 void validityChanged (const QIWidgetValidator *aValidator); 53 void isValidRequested (QIWidgetValidator *aValidator); 45 54 46 55 public slots: 56 47 57 void revalidate() { doRevalidate(); } 48 58 49 59 private: 50 QWidget *wgt; 51 bool otherValid; 60 61 QString mCaption; 62 QWidget *mWidget; 63 bool mOtherValid; 64 65 struct Watched 66 { 67 Watched() 68 : widget (NULL), buddy (NULL) 69 , state (QValidator::Acceptable) {} 70 71 QWidget *widget; 72 QWidget *buddy; 73 QValidator::State state; 74 }; 75 76 QValueList <Watched> mWatched; 77 Watched mLastInvalid; 52 78 53 79 private slots: 54 void doRevalidate() { emit validityChanged( this ); } 80 81 void doRevalidate() { emit validityChanged (this); } 55 82 }; 56 83 -
trunk/src/VBox/Frontends/VirtualBox/src/QIWidgetValidator.cpp
r4323 r4364 22 22 #include <qlineedit.h> 23 23 #include <qcombobox.h> 24 #include <qlabel.h> 25 26 #include <iprt/assert.h> 27 28 #include "VBoxGlobal.h" 24 29 25 30 /** @class QIWidgetValidator … … 70 75 * of the given widget. 71 76 * 72 * @param widget the widget whose children should be checked 73 */ 74 QIWidgetValidator::QIWidgetValidator( 75 QWidget *widget, QObject *parent, const char *name 76 ) : 77 QObject( parent, name ), 78 wgt( widget ), 79 otherValid( true ) 77 * @param aWidget Widget whose children should be checked. 78 */ 79 QIWidgetValidator::QIWidgetValidator (QWidget *aWidget, QObject *aParent, 80 const char *aName) 81 : QObject (aParent, aName) 82 , mWidget (aWidget) 83 , mOtherValid (true) 84 { 85 rescan(); 86 } 87 88 /** 89 * Constructs a new instance that will check the validity of children 90 * of the given widget. 91 * 92 * @param aCaption Caption to use for the warning message. 93 * @param aWidget Widget whose children should be checked. 94 */ 95 QIWidgetValidator::QIWidgetValidator (const QString &aCaption, 96 QWidget *aWidget, QObject *aParent, 97 const char *aName) 98 : QObject (aParent, aName) 99 , mCaption (aCaption) 100 , mWidget (aWidget) 101 , mOtherValid (true) 80 102 { 81 103 rescan(); … … 89 111 QIWidgetValidator::~QIWidgetValidator() 90 112 { 91 wgt = 0;113 mWidget = 0; 92 114 doRevalidate(); 93 115 } … … 120 142 { 121 143 // wgt is null, we assume we're valid 122 if (! wgt)144 if (!mWidget) 123 145 return true; 124 146 125 147 QIWidgetValidator *that = const_cast <QIWidgetValidator *> (this); 126 emit that->isValidRequested ( that);148 emit that->isValidRequested (that); 127 149 if (!isOtherValid()) 128 150 return false; 129 151 130 bool valid = true; 131 132 QObjectList *list = wgt->queryList ("QLineEdit"); 133 QObjectListIterator it (*list); 134 QObject *obj; 135 while (valid && (obj = it.current()) != 0) 152 QValidator::State state = QValidator::Acceptable; 153 154 for (QValueList <Watched>::ConstIterator it = mWatched.begin(); 155 it != mWatched.end(); ++ it) 136 156 { 137 ++it; 138 if (obj->inherits ("QLineEdit")) 139 { 140 QLineEdit *le = ((QLineEdit *) obj); 157 Watched watched = *it; 158 159 if (watched.widget->inherits ("QLineEdit")) 160 { 161 QLineEdit *le = ((QLineEdit *) watched.widget); 162 Assert (le->validator()); 141 163 if (!le->validator() || !le->isEnabled()) 142 164 continue; 143 valid = le->hasAcceptableInput(); 144 } 145 else if (obj->inherits ("QComboBox")) 146 { 147 QComboBox *cb = ((QComboBox *) obj); 148 if (!cb->validator() || !cb->lineEdit() || !cb->isEnabled()) 165 int pos; 166 state = le->validator()->validate (le->text(), pos); 167 } 168 else if (watched.widget->inherits ("QComboBox")) 169 { 170 QComboBox *cb = ((QComboBox *) watched.widget); 171 Assert (cb->validator()); 172 if (!cb->validator() || !cb->isEnabled()) 149 173 continue; 150 valid = cb->lineEdit()->hasAcceptableInput(); 174 int pos; 175 state = cb->lineEdit()->validator()-> 176 validate (cb->lineEdit()->text(), pos); 177 } 178 179 if (state != QValidator::Acceptable) 180 { 181 that->mLastInvalid = watched; 182 that->mLastInvalid.state = state; 183 return false; 151 184 } 152 185 } 153 delete list; 154 155 return valid; 156 } 157 158 /** 159 * Rescans all (grand) children of the managed widget and connects itself to 160 * those that can be validated, in order to emit the validityChanged() 161 * signal to give its receiver an oportunity to do useful actions. 186 187 /* reset last invalid */ 188 that->mLastInvalid = Watched(); 189 return true; 190 } 191 192 /** 193 * Rescans all (grand) children of the managed widget and: 194 * 195 * 1) remembers all supported widgets with validators to speed up further 196 * validation; 197 * 198 * 2) connects itself to those that can be validated, in order to emit the 199 * validityChanged() signal to give its receiver an oportunity to do 200 * useful actions. 201 * 202 * Must be called every time a child widget is added or removed. 162 203 */ 163 204 void QIWidgetValidator::rescan() 164 205 { 165 if (! wgt)206 if (!mWidget) 166 207 return; 167 208 168 QObjectList *list = wgt->queryList(); 209 mWatched.clear(); 210 211 Watched watched; 212 213 QObjectList *list = mWidget->queryList(); 214 QObject *obj; 215 216 /* detect all widgets that support validation */ 169 217 QObjectListIterator it (*list); 170 QObject *obj;171 218 while ((obj = it.current()) != 0) 172 219 { … … 177 224 if (!le->validator()) 178 225 continue; 179 / / disconnect to avoid duplicate connections226 /* disconnect to avoid duplicate connections */ 180 227 disconnect (le, SIGNAL (textChanged (const QString &)), 181 228 this, SLOT (doRevalidate())); … … 188 235 if (!cb->validator() || !cb->lineEdit()) 189 236 continue; 190 / / disconnect to avoid duplicate connections237 /* disconnect to avoid duplicate connections */ 191 238 disconnect (cb, SIGNAL (textChanged (const QString &)), 192 239 this, SLOT (doRevalidate())); … … 194 241 this, SLOT (doRevalidate())); 195 242 } 243 244 watched.widget = (QWidget *) obj; 245 246 /* try to find a buddy widget in order to determine the title for 247 * the watched widget which is used in the warning text */ 248 QObjectListIterator it2 (*list); 249 while ((obj = it2.current()) != 0) 250 { 251 ++ it2; 252 if (obj->inherits ("QLabel")) 253 { 254 QLabel *label = (QLabel *) obj; 255 if (label->buddy() == watched.widget) 256 { 257 watched.buddy = label; 258 break; 259 } 260 } 261 } 262 263 /* memorize */ 264 mWatched << watched; 196 265 } 266 267 /* don't forget to delete the list */ 197 268 delete list; 269 } 270 271 /** 272 * Returns a message that describes the last detected error (invalid or 273 * incomplete input). 274 * 275 * This message uses the caption text passed to the constructor as a page 276 * name to refer to. If the caption is NULL, this funciton will return a null 277 * string. 278 * 279 * Also, if the failed widget has a buddy widget, this buddy widget's text 280 * will be used as a field name to refer to. 281 */ 282 QString QIWidgetValidator::warningText() const 283 { 284 /* cannot generate an informative message if no caption provided */ 285 if (mCaption.isEmpty()) 286 return QString::null; 287 288 if (mLastInvalid.state == QValidator::Acceptable) 289 return QString::null; 290 291 AssertReturn (mLastInvalid.widget, QString::null); 292 293 QString title; 294 if (mLastInvalid.buddy != NULL) 295 { 296 if (mLastInvalid.buddy->inherits ("QLabel")) 297 title = VBoxGlobal:: 298 removeAccelMark (((QLabel *) mLastInvalid.buddy)->text()); 299 } 300 301 QString state; 302 if (mLastInvalid.state == QValidator::Intermediate) 303 state = tr ("not complete", "value state"); 304 else 305 state = tr ("invalid", "value state"); 306 307 if (!title.isEmpty()) 308 return tr ("<qt>Value of the <b>%1</b> field " 309 "on the <b>%2</b> page is %3.</qt>") 310 .arg (title, mCaption, state); 311 312 return tr ("<qt>One of the values " 313 "on the <b>%1</b> page is %2.</qt>") 314 .arg (mCaption, state); 198 315 } 199 316 -
trunk/src/VBox/Frontends/VirtualBox/ui/VBoxGlobalSettingsDlg.ui
r4071 r4364 1358 1358 <functions> 1359 1359 <function access="private">init()</function> 1360 <function access="private" returnType="QString">pagePath( QWidget * )</function> 1360 1361 <function access="private" returnType="bool">event( QEvent * )</function> 1361 1362 <function access="protected">showEvent( QShowEvent * )</function> -
trunk/src/VBox/Frontends/VirtualBox/ui/VBoxGlobalSettingsDlg.ui.h
r4071 r4364 340 340 /* General page */ 341 341 342 /// @todo (dmik) remove 343 // leVDIFolder->setValidator (new QRegExpValidator (QRegExp (".+"), this)); 344 // leMachineFolder->setValidator (new QRegExpValidator (QRegExp (".+"), this)); 345 346 wvalGeneral = new QIWidgetValidator (pageGeneral, this); 342 wvalGeneral = new QIWidgetValidator (pagePath (pageGeneral), pageGeneral, this); 347 343 connect (wvalGeneral, SIGNAL (validityChanged (const QIWidgetValidator *)), 348 344 this, SLOT (enableOk( const QIWidgetValidator *))); … … 350 346 /* Keyboard page */ 351 347 352 wvalKeyboard = new QIWidgetValidator ( pageKeyboard, this);348 wvalKeyboard = new QIWidgetValidator (pagePath (pageKeyboard), pageKeyboard, this); 353 349 connect (wvalKeyboard, SIGNAL (validityChanged (const QIWidgetValidator *)), 354 350 this, SLOT (enableOk( const QIWidgetValidator *))); … … 432 428 } 433 429 430 /** 431 * Returns a path to the given page of this settings dialog. See ::path() for 432 * details. 433 */ 434 QString VBoxGlobalSettingsDlg::pagePath (QWidget *aPage) 435 { 436 QListViewItem *li = listView-> 437 findItem (QString::number (widgetStack->id (aPage)), 1); 438 return ::path (li); 439 } 440 434 441 bool VBoxGlobalSettingsDlg::event (QEvent *aEvent) 435 442 { … … 526 533 Q_UNUSED (wval); 527 534 535 /* reset the warning text; interested parties will set it during 536 * validation */ 537 setWarning (QString::null); 538 539 QString wvalWarning; 540 528 541 /* detect the overall validity */ 529 542 bool newValid = true; … … 534 547 while ((obj = it.current()) != 0) 535 548 { 536 newValid &= ((QIWidgetValidator *) obj)->isValid(); 537 ++it; 549 QIWidgetValidator *wval = (QIWidgetValidator *) obj; 550 newValid = wval->isValid(); 551 if (!newValid) 552 { 553 wvalWarning = wval->warningText(); 554 break; 555 } 556 ++ it; 538 557 } 539 558 delete l; 540 559 } 541 560 561 if (warningString.isNull() && !wvalWarning.isNull()) 562 { 563 /* try to set the generic error message when invalid but no specific 564 * message is provided */ 565 setWarning (wvalWarning); 566 } 567 542 568 if (valid != newValid) 543 569 { 544 570 valid = newValid; 545 571 buttonOk->setEnabled (valid); 572 /// @todo in VBoxVMSettingsDlg.ui.h, this is absent at all. Is it 573 /// really what we want? 574 #if 0 546 575 if (valid) 547 576 warningSpacer->changeSize (0, 0, QSizePolicy::Expanding); 548 577 else 549 578 warningSpacer->changeSize (0, 0); 579 #endif 550 580 warningLabel->setHidden (valid); 551 581 warningPixmap->setHidden (valid); … … 846 876 /* setup validation */ 847 877 848 QIWidgetValidator *wval = new QIWidgetValidator (settings, settings); 878 QIWidgetValidator *wval = 879 new QIWidgetValidator (pagePath (pageUSB), settings, settings); 849 880 connect (wval, SIGNAL (validityChanged (const QIWidgetValidator *)), 850 881 this, SLOT (enableOk (const QIWidgetValidator *))); -
trunk/src/VBox/Frontends/VirtualBox/ui/VBoxVMSettingsDlg.ui
r4354 r4364 3166 3166 <functions> 3167 3167 <function access="private">init()</function> 3168 <function access="private" returnType="QString">pagePath( QWidget * )</function> 3168 3169 <function>setup( const QString &, const QString & )</function> 3169 3170 <function>getFromMachine( const CMachine & machine )</function> -
trunk/src/VBox/Frontends/VirtualBox/ui/VBoxVMSettingsDlg.ui.h
r4324 r4364 562 562 const uint MaxVRAM = sysProps.GetMaxGuestVRAM(); 563 563 564 leName->setValidator ( new QRegExpValidator( QRegExp( ".+" ), this ));564 leName->setValidator (new QRegExpValidator (QRegExp (".+"), this)); 565 565 566 566 leRAM->setValidator (new QIntValidator (MinRAM, MaxRAM, this)); 567 567 leVRAM->setValidator (new QIntValidator (MinVRAM, MaxVRAM, this)); 568 568 569 wvalGeneral = new QIWidgetValidator ( pageGeneral, this);569 wvalGeneral = new QIWidgetValidator (pagePath (pageGeneral), pageGeneral, this); 570 570 connect (wvalGeneral, SIGNAL (validityChanged (const QIWidgetValidator *)), 571 571 this, SLOT(enableOk (const QIWidgetValidator *))); … … 614 614 "and allows to quickly select a different hard disk.")); 615 615 616 wvalHDD = new QIWidgetValidator ( pageHDD, this);616 wvalHDD = new QIWidgetValidator (pagePath (pageHDD), pageHDD, this); 617 617 connect (wvalHDD, SIGNAL (validityChanged (const QIWidgetValidator *)), 618 618 this, SLOT (enableOk (const QIWidgetValidator *))); … … 649 649 "drive and allows to quickly select a different image.")); 650 650 651 wvalDVD = new QIWidgetValidator (page DVD, this);651 wvalDVD = new QIWidgetValidator (pagePath (pageDVD), pageDVD, this); 652 652 connect (wvalDVD, SIGNAL (validityChanged (const QIWidgetValidator *)), 653 653 this, SLOT (enableOk (const QIWidgetValidator *))); … … 675 675 "drive and allows to quickly select a different image.")); 676 676 677 wvalFloppy = new QIWidgetValidator (page Floppy, this);677 wvalFloppy = new QIWidgetValidator (pagePath (pageFloppy), pageFloppy, this); 678 678 connect (wvalFloppy, SIGNAL (validityChanged (const QIWidgetValidator *)), 679 679 this, SLOT (enableOk (const QIWidgetValidator *))); … … 761 761 "using a standard RDP client.")); 762 762 763 ULONG maxPort = 65535; 764 leVRDPPort->setValidator (new QIntValidator (0, maxPort, this)); 765 leVRDPTimeout->setValidator (new QIntValidator (0, maxPort, this)); 766 wvalVRDP = new QIWidgetValidator (pageVRDP, this); 763 leVRDPPort->setValidator (new QIntValidator (0, 0xFFFF, this)); 764 leVRDPTimeout->setValidator (new QIntValidator (this)); 765 wvalVRDP = new QIWidgetValidator (pagePath (pageVRDP), pageVRDP, this); 767 766 connect (wvalVRDP, SIGNAL (validityChanged (const QIWidgetValidator *)), 768 767 this, SLOT (enableOk (const QIWidgetValidator *))); … … 878 877 cbVRDPAuthType->insertItem (vboxGlobal().toString (CEnums::VRDPAuthGuest)); 879 878 leVRDPTimeout->setAlignment (Qt::AlignRight); 879 } 880 881 /** 882 * Returns a path to the given page of this settings dialog. See ::path() for 883 * details. 884 */ 885 QString VBoxVMSettingsDlg::pagePath (QWidget *aPage) 886 { 887 QListViewItem *li = listView-> 888 findItem (QString::number (widgetStack->id (aPage)), 1); 889 return ::path (li); 880 890 } 881 891 … … 1298 1308 1299 1309 1300 void VBoxVMSettingsDlg::enableOk ( const QIWidgetValidator *wval)1310 void VBoxVMSettingsDlg::enableOk (const QIWidgetValidator *wval) 1301 1311 { 1302 1312 Q_UNUSED (wval); … … 1305 1315 * validation */ 1306 1316 setWarning (QString::null); 1317 1318 QString wvalWarning; 1307 1319 1308 1320 /* detect the overall validity */ … … 1314 1326 while ((obj = it.current()) != 0) 1315 1327 { 1316 newValid &= ((QIWidgetValidator *) obj)->isValid(); 1317 ++it; 1328 QIWidgetValidator *wval = (QIWidgetValidator *) obj; 1329 newValid = wval->isValid(); 1330 if (!newValid) 1331 { 1332 wvalWarning = wval->warningText(); 1333 break; 1334 } 1335 ++ it; 1318 1336 } 1319 1337 delete l; 1338 } 1339 1340 if (warningString.isNull() && !wvalWarning.isNull()) 1341 { 1342 /* try to set the generic error message when invalid but no specific 1343 * message is provided */ 1344 setWarning (wvalWarning); 1320 1345 } 1321 1346 … … 1337 1362 1338 1363 QString warningText; 1339 QString pageTitle = ::path (listView->currentItem());1364 QString pageTitle = pagePath (pg); 1340 1365 1341 1366 if (pg == pageHDD) … … 1506 1531 } 1507 1532 } 1508 }1509 else if (pg == pageVRDP)1510 {1511 if (pageVRDP->isEnabled())1512 {1513 valid = !(grbVRDP->isChecked() &&1514 (leVRDPPort->text().isEmpty() || leVRDPTimeout->text().isEmpty()));1515 if (!valid && leVRDPPort->text().isEmpty())1516 warningText = tr ("VRDP Port is not set ");1517 if (!valid && leVRDPTimeout->text().isEmpty())1518 warningText = tr ("VRDP Timeout is not set ");1519 }1520 else1521 valid = true;1522 1533 } 1523 1534 … … 2191 2202 page->loadList (mInterfaceList, mNoInterfaces); 2192 2203 page->getFromAdapter (aAdapter); 2193 tbwNetwork->addTab (page, QString (tr ("Adapter %1", "network")) 2194 .arg (aAdapter.GetSlot())); 2204 QString pageTitle = QString (tr ("Adapter %1", "network")) 2205 .arg (aAdapter.GetSlot()); 2206 tbwNetwork->addTab (page, pageTitle); 2195 2207 2196 2208 /* fix the tab order so that main dialog's buttons are always the last */ … … 2200 2212 2201 2213 /* setup validation */ 2202 QIWidgetValidator *wval = new QIWidgetValidator (pageNetwork, this); 2214 QIWidgetValidator *wval = 2215 new QIWidgetValidator (QString ("%1: %2") 2216 .arg (pagePath (pageNetwork), pageTitle), 2217 pageNetwork, this); 2203 2218 connect (page->grbEnabled, SIGNAL (toggled (bool)), wval, SLOT (revalidate())); 2204 2219 connect (page->cbNetworkAttachment, SIGNAL (activated (const QString &)), … … 2228 2243 VBoxVMSerialPortSettings *page = new VBoxVMSerialPortSettings(); 2229 2244 page->getFromPort (aPort); 2230 tbwSerialPorts->addTab (page, QString (tr ("Port %1", "serial ports")) 2231 .arg (aPort.GetSlot())); 2245 QString pageTitle = QString (tr ("Port %1", "serial ports")) 2246 .arg (aPort.GetSlot()); 2247 tbwSerialPorts->addTab (page, pageTitle); 2232 2248 2233 2249 /* fix the tab order so that main dialog's buttons are always the last */ … … 2237 2253 2238 2254 /* setup validation */ 2239 QIWidgetValidator *wval = new QIWidgetValidator (pageSerial, this); 2255 QIWidgetValidator *wval = 2256 new QIWidgetValidator (QString ("%1: %2") 2257 .arg (pagePath (pageSerial), pageTitle), 2258 pageSerial, this); 2240 2259 connect (page->mSerialPortBox, SIGNAL (toggled (bool)), 2241 2260 wval, SLOT (revalidate())); … … 2355 2374 /* setup validation */ 2356 2375 2357 QIWidgetValidator *wval = new QIWidgetValidator (settings, settings); 2376 QIWidgetValidator *wval = 2377 new QIWidgetValidator (pagePath (pageUSB), settings, settings); 2358 2378 connect (wval, SIGNAL (validityChanged (const QIWidgetValidator *)), 2359 2379 this, SLOT (enableOk (const QIWidgetValidator *)));
Note:
See TracChangeset
for help on using the changeset viewer.