2015-02-07 00:32:25 +00:00
/*************************************************************************
* Copyright ( C ) 2008 , 2009 by Volker Lanz < vl @ fidra . de > *
2016-03-02 18:33:40 +00:00
* Copyright ( C ) 2016 by Andrius Š tikonas < andrius @ stikonas . eu > *
2015-02-07 00:32:25 +00:00
* *
* This program is free software ; you can redistribute it and / or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation ; either version 3 of *
* the License , or ( at your option ) any later version . *
* *
* This program is distributed in the hope that it will be useful , *
* but WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the *
* GNU General Public License for more details . *
* *
* You should have received a copy of the GNU General Public License *
* along with this program . If not , see < http : //www.gnu.org/licenses/>.*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-04-22 13:41:30 +01:00
# include "gui/treelog.h"
# include "gui/partitionmanagerwidget.h"
2016-01-12 00:12:49 +00:00
# include <util/globallog.h>
# include <util/helpers.h>
2009-04-22 13:41:30 +01:00
2014-05-18 21:41:48 +01:00
# include <QDateTime>
2014-05-13 14:47:35 +01:00
# include <QDebug>
2010-03-07 19:02:02 +00:00
# include <QFile>
2014-05-18 21:41:48 +01:00
# include <QFileDialog>
# include <QMenu>
# include <QTemporaryFile>
# include <QTextStream>
2009-04-22 13:41:30 +01:00
# include <QTreeWidget>
2010-03-07 19:02:02 +00:00
# include <QTreeWidgetItem>
2014-05-18 21:41:48 +01:00
2015-04-09 21:26:40 +01:00
# include <KIconLoader>
2014-05-18 21:41:48 +01:00
# include <KIO/CopyJob>
# include <KJobUiDelegate>
# include <KLocalizedString>
# include <KMessageBox>
2010-03-07 19:02:02 +00:00
# include <config.h>
2009-04-22 13:41:30 +01:00
/** Creates a new TreeLog instance.
2015-07-13 15:17:41 +01:00
@ param parent the parent widget
2009-04-22 13:41:30 +01:00
*/
TreeLog : : TreeLog ( QWidget * parent ) :
2015-07-13 15:17:41 +01:00
QWidget ( parent ) ,
Ui : : TreeLogBase ( )
2009-04-22 13:41:30 +01:00
{
2015-07-13 15:17:41 +01:00
setupUi ( this ) ;
2010-03-07 19:02:02 +00:00
2015-07-13 15:17:41 +01:00
treeLog ( ) . header ( ) - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2016-05-18 22:23:59 +01:00
connect ( treeLog ( ) . header ( ) , & QHeaderView : : customContextMenuRequested , this , & TreeLog : : onHeaderContextMenu ) ;
2010-03-07 19:02:02 +00:00
}
TreeLog : : ~ TreeLog ( )
{
2015-07-13 15:17:41 +01:00
saveConfig ( ) ;
2010-03-07 19:02:02 +00:00
}
void TreeLog : : init ( )
{
2015-07-13 15:17:41 +01:00
loadConfig ( ) ;
2010-03-07 19:02:02 +00:00
}
void TreeLog : : loadConfig ( )
{
2015-07-13 15:17:41 +01:00
QList < int > colWidths = Config : : treeLogColumnWidths ( ) ;
QList < int > colPositions = Config : : treeLogColumnPositions ( ) ;
QList < int > colVisible = Config : : treeLogColumnVisible ( ) ;
QHeaderView * header = treeLog ( ) . header ( ) ;
for ( int i = 0 ; i < treeLog ( ) . columnCount ( ) ; i + + ) {
if ( colPositions [ 0 ] ! = - 1 & & colPositions . size ( ) > i )
header - > moveSection ( header - > visualIndex ( i ) , colPositions [ i ] ) ;
if ( colVisible [ 0 ] ! = - 1 & & colVisible . size ( ) > i )
treeLog ( ) . setColumnHidden ( i , colVisible [ i ] = = 0 ) ;
if ( colWidths [ 0 ] ! = - 1 & & colWidths . size ( ) > i )
treeLog ( ) . setColumnWidth ( i , colWidths [ i ] ) ;
}
2010-03-07 19:02:02 +00:00
}
void TreeLog : : saveConfig ( ) const
{
2015-07-13 15:17:41 +01:00
QList < int > colWidths ;
QList < int > colPositions ;
QList < int > colVisible ;
for ( int i = 0 ; i < treeLog ( ) . columnCount ( ) ; i + + ) {
colPositions . append ( treeLog ( ) . header ( ) - > visualIndex ( i ) ) ;
colVisible . append ( treeLog ( ) . isColumnHidden ( i ) ? 0 : 1 ) ;
colWidths . append ( treeLog ( ) . columnWidth ( i ) ) ;
}
Config : : setTreeLogColumnPositions ( colPositions ) ;
Config : : setTreeLogColumnVisible ( colVisible ) ;
Config : : setTreeLogColumnWidths ( colWidths ) ;
Config : : self ( ) - > save ( ) ;
2010-03-07 19:02:02 +00:00
}
void TreeLog : : onHeaderContextMenu ( const QPoint & pos )
{
2015-07-13 15:17:41 +01:00
showColumnsContextMenu ( pos , treeLog ( ) ) ;
2010-03-07 19:02:02 +00:00
}
void TreeLog : : onClearLog ( )
{
2015-07-13 15:17:41 +01:00
while ( QTreeWidgetItem * item = treeLog ( ) . takeTopLevelItem ( 0 ) )
delete item ;
2010-03-07 19:02:02 +00:00
}
void TreeLog : : onSaveLog ( )
{
2015-07-13 15:17:41 +01:00
const QUrl url = QFileDialog : : getSaveFileUrl ( ) ;
2010-03-07 19:02:02 +00:00
2015-07-13 15:17:41 +01:00
if ( ! url . isEmpty ( ) ) {
QTemporaryFile tempFile ;
2010-03-07 19:02:02 +00:00
2015-07-13 15:17:41 +01:00
if ( ! tempFile . open ( ) ) {
KMessageBox : : error ( this , xi18nc ( " @info " , " Could not create temporary output file to save <filename>%1</filename>. " , url . fileName ( ) ) , i18nc ( " @title:window " , " Error Saving Log File " ) ) ;
return ;
}
2010-03-07 19:02:02 +00:00
2015-07-13 15:17:41 +01:00
QTextStream stream ( & tempFile ) ;
2010-03-07 19:02:02 +00:00
2015-07-13 15:17:41 +01:00
for ( qint32 idx = 0 ; idx < treeLog ( ) . topLevelItemCount ( ) ; idx + + ) {
QTreeWidgetItem * item = treeLog ( ) . topLevelItem ( idx ) ;
stream < < item - > text ( 1 ) < < " : " < < item - > text ( 2 ) < < " \n " ;
}
2010-04-11 15:29:20 +01:00
2015-07-13 15:17:41 +01:00
tempFile . close ( ) ;
2010-04-11 15:29:20 +01:00
2015-07-13 15:17:41 +01:00
KIO : : CopyJob * job = KIO : : move ( QUrl : : fromLocalFile ( tempFile . fileName ( ) ) , url , KIO : : HideProgressInfo ) ;
job - > exec ( ) ;
if ( job - > error ( ) )
job - > ui ( ) - > showErrorMessage ( ) ;
}
2010-03-07 19:02:02 +00:00
}
void TreeLog : : on_m_TreeLog_customContextMenuRequested ( const QPoint & pos )
{
2015-07-13 15:17:41 +01:00
emit contextMenuRequested ( treeLog ( ) . viewport ( ) - > mapToGlobal ( pos ) ) ;
2009-04-22 13:41:30 +01:00
}
2010-01-26 21:12:42 +00:00
void TreeLog : : onNewLogMessage ( Log : : Level logLevel , const QString & s )
2009-04-22 13:41:30 +01:00
{
2015-07-13 15:17:41 +01:00
static const QString icons [ ] = {
QStringLiteral ( " tools-report-bug " ) ,
QStringLiteral ( " dialog-information " ) ,
QStringLiteral ( " dialog-warning " ) ,
QStringLiteral ( " dialog-error " )
} ;
qDebug ( ) < < s ;
if ( logLevel > = Config : : minLogLevel ( ) ) {
QTreeWidgetItem * item = new QTreeWidgetItem ( ) ;
item - > setIcon ( 0 , QIcon : : fromTheme ( icons [ logLevel ] ) . pixmap ( IconSize ( KIconLoader : : Small ) ) ) ;
item - > setText ( 1 , QDateTime : : currentDateTime ( ) . toString ( QStringLiteral ( " yyyy-MM-dd hh:mm:ss " ) ) ) ;
item - > setText ( 2 , s ) ;
treeLog ( ) . addTopLevelItem ( item ) ;
treeLog ( ) . scrollToBottom ( ) ;
}
2009-04-22 13:41:30 +01:00
}