VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxVMLogViewer.ui.h@ 2988

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

InnoTek -> innotek part 4: more miscellaneous files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 9.5 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * "Virtual Log Viewer" dialog UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2006 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.215389.xyz. 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 wish to add, delete or rename functions or slots use
27** Qt Designer which will update this file, preserving your code. Create an
28** init() function in place of a constructor, and a destroy() function in
29** place of a destructor.
30*****************************************************************************/
31
32
33void VBoxVMLogViewer::init()
34{
35 /* dialog initially is not polished */
36 mIsPolished = false;
37
38 /* search the default button */
39 mDefaultButton = searchDefaultButton();
40 qApp->installEventFilter (this);
41
42 /* setup a dialog icon */
43 setIcon (QPixmap::fromMimeSource ("show_logs_16px.png"));
44
45 /* statusbar initially disabled */
46 statusBar()->setHidden (true);
47
48 /* setup size grip */
49 mSizeGrip = new QSizeGrip (centralWidget(), "mSizeGrip");
50 mSizeGrip->resize (mSizeGrip->sizeHint());
51 mSizeGrip->stackUnder (mCloseButton);
52
53 /* logs list creation */
54 mLogList = new QTabWidget (mLogsFrame, "mLogList");
55 QVBoxLayout *logsFrameLayout = new QVBoxLayout (mLogsFrame);
56 logsFrameLayout->addWidget (mLogList);
57
58 /* applying language settings */
59 languageChangeImp();
60}
61
62
63void VBoxVMLogViewer::destroy()
64{
65 /* switch the related toggle action off */
66 mAction->setOn (false);
67}
68
69
70void VBoxVMLogViewer::languageChangeImp()
71{
72 /* setup a dialog caption */
73 if (!mMachine.isNull())
74 setCaption (tr ("%1 - VirtualBox Log Viewer").arg (mMachine.GetName()));
75}
76
77
78QPushButton* VBoxVMLogViewer::searchDefaultButton()
79{
80 /* this mechanism is used for searching the default dialog button
81 * and similar the same mechanism in Qt::QDialog inner source */
82 QPushButton *button = 0;
83 QObjectList *list = queryList ("QPushButton");
84 QObjectListIt it (*list);
85 while ((button = (QPushButton*)it.current()) && !button->isDefault())
86 ++ it;
87 return button;
88}
89
90
91bool VBoxVMLogViewer::eventFilter (QObject *aObject, QEvent *aEvent)
92{
93 switch (aEvent->type())
94 {
95 /* auto-default button focus-in processor used to move the "default"
96 * button property into the currently focused button */
97 case QEvent::FocusIn:
98 {
99 if (aObject->inherits ("QPushButton") &&
100 aObject->parent() == centralWidget())
101 {
102 ((QPushButton*)aObject)->setDefault (aObject != mDefaultButton);
103 if (mDefaultButton)
104 mDefaultButton->setDefault (aObject == mDefaultButton);
105 }
106 break;
107 }
108 /* auto-default button focus-out processor used to remove the "default"
109 * button property from the previously focused button */
110 case QEvent::FocusOut:
111 {
112 if (aObject->inherits ("QPushButton") &&
113 aObject->parent() == centralWidget())
114 {
115 if (mDefaultButton)
116 mDefaultButton->setDefault (aObject != mDefaultButton);
117 ((QPushButton*)aObject)->setDefault (aObject == mDefaultButton);
118 }
119 break;
120 }
121 default:
122 break;
123 }
124 return QMainWindow::eventFilter (aObject, aEvent);
125}
126
127
128bool VBoxVMLogViewer::event (QEvent *aEvent)
129{
130 bool result = QMainWindow::event (aEvent);
131 switch (aEvent->type())
132 {
133 case QEvent::LanguageChange:
134 {
135 languageChangeImp();
136 break;
137 }
138 default:
139 break;
140 }
141 return result;
142}
143
144
145void VBoxVMLogViewer::keyPressEvent (QKeyEvent *aEvent)
146{
147 if (aEvent->state() == 0 ||
148 (aEvent->state() & Keypad && aEvent->key() == Key_Enter))
149 {
150 switch (aEvent->key())
151 {
152 /* processing the return keypress for the auto-default button */
153 case Key_Enter:
154 case Key_Return:
155 {
156 QPushButton *currentDefault = searchDefaultButton();
157 if (currentDefault)
158 currentDefault->animateClick();
159 break;
160 }
161 /* processing the escape keypress as the close dialog action */
162 case Key_Escape:
163 {
164 close();
165 break;
166 }
167 }
168 }
169 else
170 aEvent->ignore();
171}
172
173
174void VBoxVMLogViewer::showEvent (QShowEvent *aEvent)
175{
176 QMainWindow::showEvent (aEvent);
177
178 /* one may think that QWidget::polish() is the right place to do things
179 * below, but apparently, by the time when QWidget::polish() is called,
180 * the widget style & layout are not fully done, at least the minimum
181 * size hint is not properly calculated. Since this is sometimes necessary,
182 * we provide our own "polish" implementation. */
183
184 if (mIsPolished)
185 return;
186
187 mIsPolished = true;
188
189 VBoxGlobal::centerWidget (this, parentWidget());
190}
191
192
193void VBoxVMLogViewer::resizeEvent (QResizeEvent*)
194{
195 /* adjust the size-grip location for the current resize event */
196 mSizeGrip->move (centralWidget()->rect().bottomRight() -
197 QPoint (mSizeGrip->rect().width() - 1,
198 mSizeGrip->rect().height() - 1));
199}
200
201
202void VBoxVMLogViewer::setup (CMachine &aMachine, QAction *aAction)
203{
204 /* saving predefined variables */
205 mMachine = aMachine;
206 mAction = aAction;
207
208 /* setup self-destruction handler */
209 connect (mAction, SIGNAL (toggled (bool)), this, SLOT (suicide (bool)));
210
211 /* reading log files */
212 refresh();
213
214 /* loading language constants */
215 languageChangeImp();
216}
217
218
219void VBoxVMLogViewer::refresh()
220{
221 /* clearing old data if any */
222 mLogFilesList.clear();
223 while (mLogList->count())
224 {
225 QWidget *logPage = mLogList->page (0);
226 mLogList->removePage (logPage);
227 delete logPage;
228 }
229
230 bool isAnyLogPresent = false;
231
232 /* entering log files folder */
233 QString logFilesPath = mMachine.GetLogFolder();
234 QDir logFilesDir (logFilesPath);
235 if (logFilesDir.exists())
236 {
237 /* reading log files folder */
238 QStringList logList = logFilesDir.entryList (QDir::Files);
239 if (!logList.empty()) isAnyLogPresent = true;
240 for (QStringList::Iterator it = logList.begin(); it != logList.end(); ++it)
241 loadLogFile (logFilesDir.filePath (*it));
242 }
243
244 /* creating clean log page if there is no logs at all */
245 if (!isAnyLogPresent)
246 {
247 QTextBrowser *dummyLog = createLogPage ("VBox.log");
248 dummyLog->setTextFormat (Qt::RichText);
249 dummyLog->setText (tr ("<p>No log files found. Press the <b>Refresh</b> "
250 "button to rescan the log folder <nobr><b>%1</b></nobr>.</p>")
251 .arg (logFilesPath));
252 }
253
254 /* show the first tab widget's page after the refresh */
255 mLogList->showPage (mLogList->page(0));
256 mSaveButton->setEnabled (isAnyLogPresent);
257}
258
259
260void VBoxVMLogViewer::loadLogFile (const QString &aFileName)
261{
262 /* prepare log file */
263 QFile logFile (aFileName);
264 if (!logFile.exists() || !logFile.open (IO_ReadOnly))
265 return;
266
267 /* read log file and write it into the log page */
268 QTextBrowser *logViewer = createLogPage (QFileInfo (aFileName).fileName());
269 logViewer->setText (logFile.readAll());
270
271 mLogFilesList << aFileName;
272}
273
274
275QTextBrowser* VBoxVMLogViewer::createLogPage (const QString &aName)
276{
277 QTextBrowser *logViewer = new QTextBrowser();
278 logViewer->setTextFormat (Qt::PlainText);
279 QFont font = logViewer->currentFont();
280 font.setFamily ("Courier New,courier");
281 logViewer->setFont (font);
282 mLogList->addTab (logViewer, aName);
283 return logViewer;
284}
285
286
287void VBoxVMLogViewer::suicide (bool aNo)
288{
289 if (!aNo) close();
290}
291
292
293void VBoxVMLogViewer::save()
294{
295 /* prepare "save as" dialog */
296 QDate date = QDate::currentDate();
297 QTime time = QTime::currentTime();
298 QString defaultFileName = QString ("%1-%2-%3-%4-%5-%6-%7.log")
299 .arg (mMachine.GetName())
300 .arg (date.year()).arg (date.month()).arg (date.day())
301 .arg (time.hour()).arg (time.minute()).arg (time.second());
302 QString defaultFullName = QDir::convertSeparators (QDir::home().absPath() + "/" +
303 defaultFileName);
304
305 QString newFileName = QFileDialog::getSaveFileName (defaultFullName,
306 QString::null, this, "SaveLogAsDialog", tr ("Save VirtualBox Log As"));
307
308 /* save new log into the file */
309 if (!newFileName.isEmpty())
310 {
311 /* reread log data */
312 QFile oldFile (mLogFilesList [mLogList->currentPageIndex()]);
313 QFile newFile (newFileName);
314 if (!oldFile.open (IO_ReadOnly) || !newFile.open (IO_WriteOnly))
315 return;
316
317 /* save log data into the new file */
318 newFile.writeBlock (oldFile.readAll());
319 }
320}
321
Note: See TracBrowser for help on using the repository browser.

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