]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
Fix messages layout bug
[vlc] / modules / gui / qt4 / dialogs / messages.cpp
1 /*****************************************************************************
2  * Messages.cpp : Information about an item
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: Messages.cpp 16024 2006-07-13 13:51:05Z xtophe $
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "input_manager.hpp"
25 #include "dialogs/messages.hpp"
26 #include "dialogs_provider.hpp"
27 #include "util/qvlcframe.hpp"
28 #include "qt4.hpp"
29
30 MessagesDialog *MessagesDialog::instance = NULL;
31
32 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf, bool _main_input ) :
33                               QVLCFrame( _p_intf ), main_input( _main_input )
34 {
35     setWindowTitle( _("Messages" ) );
36     resize(600, 400);
37
38     QGridLayout *layout = new QGridLayout(this);
39     QPushButton *closeButton = new QPushButton(qtr("&Close"));
40     QPushButton *clearButton = new QPushButton(qtr("&Clear"));
41     QPushButton *saveLogButton = new QPushButton(qtr("&Save as..."));
42     QSpinBox *verbosityBox = new QSpinBox();
43     verbosityBox->setRange(1, 3);
44     verbosityBox->setWrapping(true);
45     QLabel *verbosityLabel = new QLabel(qtr("Verbosity Level"));
46     messages = new QTextEdit();
47     messages->setReadOnly(true);
48     messages->setGeometry(0, 0, 440, 600);
49     messages->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50
51     layout->addWidget(messages, 0, 0, 1, 0);
52     layout->addWidget(verbosityLabel, 1, 0, 1, 1);
53     layout->addWidget(verbosityBox, 1, 2);
54     layout->addWidget(saveLogButton, 2, 0);
55     layout->addWidget(clearButton, 2, 1);
56     layout->addWidget(closeButton, 2, 2);
57
58     connect( closeButton, SIGNAL( clicked() ) ,
59            this, SLOT( onCloseButton()));
60     connect( clearButton, SIGNAL( clicked() ) ,
61            this, SLOT( onClearButton()));
62     connect( saveLogButton, SIGNAL( clicked() ) ,
63            this, SLOT( onSaveButton()));
64     connect( verbosityBox, SIGNAL( valueChanged(int) ),
65            this, SLOT( onVerbosityChanged(int)));
66     connect( DialogsProvider::getInstance(NULL)->fixed_timer,
67              SIGNAL( timeout() ), this, SLOT(updateLog() ) );
68
69     p_input = NULL;
70 }
71
72 MessagesDialog::~MessagesDialog()
73 {
74 }
75
76 void MessagesDialog::updateLog()
77 {
78     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
79     int i_start;
80
81     vlc_mutex_lock( p_sub->p_lock );
82     int i_stop = *p_sub->pi_stop;
83     vlc_mutex_unlock( p_sub->p_lock );
84
85     if( p_sub->i_start != i_stop )
86     {
87         for( i_start = p_sub->i_start;
88                 i_start != i_stop;
89                 i_start = (i_start+1) % VLC_MSG_QSIZE )
90         {
91           // [FIXME] Does not work as the old one
92           // Outputs too much data ?
93           // if (p_sub->p_msg[i_start].i_type = VLC_MSG_ERR)
94           //          continue;
95           //  if( !b_verbose &&
96           //         VLC_MSG_ERR != p_sub->p_msg[i_start].i_type )
97           //                continue;
98
99             /* Append all messages to log window */
100
101
102             messages->setFontItalic(true);
103             messages->setTextColor("darkBlue");
104             messages->insertPlainText(p_sub->p_msg[i_start].psz_module);
105
106             switch( p_sub->p_msg[i_start].i_type )
107             {
108                 case VLC_MSG_INFO:
109                     messages->setTextColor("blue");
110                     messages->insertPlainText(": ");
111                     break;
112                 case VLC_MSG_ERR:
113                     messages->setTextColor("red");
114                     messages->insertPlainText(" error: ");
115                     break;
116                 case VLC_MSG_WARN:
117                     messages->setTextColor("green");
118                     messages->insertPlainText(" warning: ");
119                     break;
120                 case VLC_MSG_DBG:
121                 default:
122                     messages->setTextColor("grey");
123                     messages->insertPlainText(" debug: ");
124                     break;
125             }
126
127             /* Add message Regular black Font */
128             messages->setFontItalic(false);
129             messages->setTextColor("black");
130             messages->insertPlainText( p_sub->p_msg[i_start].psz_msg );
131             messages->insertPlainText( "\n" );
132         }
133         messages->ensureCursorVisible();
134
135         vlc_mutex_lock( p_sub->p_lock );
136         p_sub->i_start = i_start;
137         vlc_mutex_unlock( p_sub->p_lock );
138     }
139 }
140
141 void MessagesDialog::onCloseButton()
142 {
143     this->toggleVisible();
144 }
145
146 void MessagesDialog::onClearButton()
147 {
148     messages->clear();
149 }
150
151 bool MessagesDialog::onSaveButton()
152 {
153     QString saveLogFileName = QFileDialog::getSaveFileName(
154             this,
155             "Choose a filename to save the logs under...",
156             p_intf->p_vlc->psz_homedir,
157             "Texts / Logs (*.log *.txt);; All (*.*) ");
158
159     if (saveLogFileName != NULL)
160     {
161         QFile file(saveLogFileName);
162         if (!file.open(QFile::WriteOnly | QFile::Text)) {
163             QMessageBox::warning(this, qtr("Application"),
164                     qtr("Cannot write file %1:\n%2.")
165                     .arg(saveLogFileName)
166                     .arg(file.errorString()));
167             return false;
168         }
169
170         QTextStream out(&file);
171         out << messages->toPlainText() << "\n";
172
173         return true;
174     }
175     return false;
176 }
177
178 void MessagesDialog::onVerbosityChanged(int verbosityLevel)
179 {
180     //FIXME: Does not seems to work.
181     vlc_value_t  val;
182     val.i_int = verbosityLevel - 1;
183     var_Set( p_intf->p_vlc, "verbose", val );
184 }
185