File: DiffViewer.cpp

package info (click to toggle)
bsc 2.27-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,640 kB
  • ctags: 2,610
  • sloc: cpp: 14,100; perl: 114; makefile: 46
file content (145 lines) | stat: -rw-r--r-- 5,127 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/********************************************************************
 * Copyright (C) 2005, 2006 Piotr Pszczolkowski
 *-------------------------------------------------------------------
 * This file is part of BSCommander (Beesoft Commander).
 *
 * BsC 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 2 of the License, or
 * (at your option) any later version.
 *
 * BsC 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 BsC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *******************************************************************/

/*------- include files:
-------------------------------------------------------------------*/
#include "DiffViewer.h"
#include "Shared.h"
#include "Busy.h"
#include "Config.h"
#include "SystemCall.h"
#include <qtextbrowser.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qfiledialog.h>
#include <vector>
using namespace std;

/*------- local constants:
-------------------------------------------------------------------*/
const QString DiffViewer::Caption     = QT_TR_NOOP( "Diff viewer" );
const QString DiffViewer::FileMusters = QT_TR_NOOP( "Text file (*.txt)" );


//*******************************************************************
// DiffViewer                                            CONSTRUCTOR
//*******************************************************************
DiffViewer::DiffViewer( QWidget* const in_parent, const QString& in_lfile, const QString& in_rfile )
: QDialog      ( in_parent )
, d_main_layout( new QVBoxLayout( this ))
, d_btn_layout ( new QHBoxLayout )
, d_browser    ( new QTextBrowser( this ))
, d_save_btn   ( new QPushButton( tr(Shared::SaveBtnLabel), this ))
, d_close_btn  ( new QPushButton( tr(Shared::CloseBtnLabel), this ))
, d_syscall    ( new SystemCall( this ))
{
	setCaption( tr(Caption) );
	setFont( Config::instance()->lfs_font() );
	
	d_main_layout->setMargin( Shared::LayoutMargin );
	d_main_layout->setSpacing( Shared::LayoutSpacing );
	
	d_browser->setPaletteBackgroundColor( Config::instance()->lfs_default_bkg_color() );
	QFontMetrics fm( font() );
	d_browser->setTabStopWidth( 3 * fm.width( "X" ));
	d_browser->show();		
	d_main_layout->addWidget( d_browser );
	
	d_btn_layout->addStretch( Shared::OverStretch );
	d_btn_layout->addWidget( d_save_btn );
	d_btn_layout->addWidget( d_close_btn );
	d_main_layout->addLayout( d_btn_layout );
	
	connect( d_save_btn , SIGNAL( clicked()       ), this, SLOT( slot_save()     ));
	connect( d_close_btn, SIGNAL( clicked()       ), this, SLOT( accept()        ));
	connect( d_syscall  , SIGNAL( finished( int ) ), this, SLOT( finished( int ) ));
	
	vector<QString> parameters;
	parameters.push_back( "diff" );
	parameters.push_back( "-u" );
	parameters.push_back( "-w" );
	parameters.push_back( in_rfile );
	parameters.push_back( in_lfile );
	d_syscall->run( parameters );
}
// end of DiffViewer

//*******************************************************************
// ~DiffViewer                                            DESTRUCTOR
//*******************************************************************
DiffViewer::~DiffViewer()
{
	if( d_syscall ) {
		delete d_syscall;
		d_syscall = 0;
	}
}
// end of ~DiffViewer

//*******************************************************************
// slot_save                                            PRIVATE slot
//*******************************************************************
void DiffViewer::slot_save()
{
	const QString fname = QFileDialog::getSaveFileName( QDir::homeDirPath(),
															tr(FileMusters), this );
	if( FALSE == fname.isEmpty() ) {
		QFile file( fname );
		if( file.open( IO_WriteOnly ) ) {
			QTextStream out( &file );
			Busy::set_busy( TRUE );
			out << d_browser->text();
			Busy::set_busy( FALSE );
			file.close();
		}
	}
}
// end of slot_save

//*******************************************************************
// polish                                          PRIVATE inherited
//*******************************************************************
void DiffViewer::polish()
{
	Shared::polish( this, 60, 60 );
}
// end of polish

//*******************************************************************
// finished                                             PRIVATE slot
//*******************************************************************
void DiffViewer::finished( int )
{
	vector<QString> stdout_lines;
	vector<QString> stderr_lines;
		
	d_syscall->result( stdout_lines, stderr_lines );
	delete d_syscall;
	d_syscall = 0;
		
	if( FALSE == stdout_lines.empty() ) {
		vector<QString>::const_iterator it = stdout_lines.begin();
		while( it != stdout_lines.end() ) {
			d_browser->append( *it );
			++it;
		}
	}
}
// end of finished