summaryrefslogtreecommitdiffstats
path: root/khexedit/converterdialog.cc
blob: f8dd9be8a22442e4b97750f8b1109864a07224ed (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *   khexedit - Versatile hex editor
 *   Copyright (C) 1999-2000 Espen Sand, [email protected]
 *
 *   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 2 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, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */


#include <qlabel.h>
#include <qlayout.h>

#include <klocale.h>

#include "converterdialog.h"
#include "hexvalidator.h"


CValidateLineEdit::CValidateLineEdit( QWidget *parent, int validateType,
				      const char *name )
  :QLineEdit( parent, name ), mBusy(false)
{
  mValidator = new CHexValidator( this, (CHexValidator::EState)validateType );
  setValidator( mValidator );
  connect( this, SIGNAL(textChanged(const QString &)),
	   this, SLOT(convertText(const QString &)) );
}


CValidateLineEdit::~CValidateLineEdit( void )
{
}


void CValidateLineEdit::setData( const QByteArray &buf )
{
  if( mBusy == false )
  {
    QString text;
    mValidator->format( text, buf );
    setText( text );
  }
}


void CValidateLineEdit::convertText( const QString &text )
{
  QByteArray buf;
  mValidator->convert( buf, text );
  mBusy = true; // Don't update while editing
  emit dataChanged( buf );
  mBusy = false;
}



CConverterDialog::CConverterDialog( QWidget *parent, const char *name, 
				    bool modal )
  :KDialogBase( parent, name, modal, i18n("Converter"), Cancel|User2|User1, 
		Cancel, true, KStdGuiItem::clear(), i18n("&On Cursor") )
{
  QWidget *page = new QWidget( this );
  setMainWidget( page );

  QGridLayout *topLayout = new QGridLayout( page, 6, 2, 0, spacingHint() );
  topLayout->setRowStretch( 5, 10 );
  topLayout->setColStretch( 1, 10 );

  QLabel *label = new QLabel( i18n("Hexadecimal:"), page );
  topLayout->addWidget( label, 0, 0 );
  label = new QLabel( i18n("Decimal:"), page );
  topLayout->addWidget( label, 1, 0 );
  label = new QLabel( i18n("Octal:"), page );
  topLayout->addWidget( label, 2, 0 );
  label = new QLabel( i18n("Binary:"), page );
  topLayout->addWidget( label, 3, 0 );
  label = new QLabel( i18n("Text:"), page );
  topLayout->addWidget( label, 4, 0 );

  mHexInput = new CValidateLineEdit( page, CHexValidator::hexadecimal );
  mHexInput->setMinimumWidth( fontMetrics().maxWidth()*17 );
  topLayout->addWidget( mHexInput, 0, 1 );
  mDecInput = new CValidateLineEdit( page, CHexValidator::decimal );
  topLayout->addWidget( mDecInput, 1, 1 );
  mOctInput = new CValidateLineEdit( page, CHexValidator::octal );
  topLayout->addWidget( mOctInput, 2, 1 );
  mBinInput = new CValidateLineEdit( page, CHexValidator::binary );
  topLayout->addWidget( mBinInput, 3, 1 );
  mTxtInput = new CValidateLineEdit( page, CHexValidator::regularText );
  topLayout->addWidget( mTxtInput, 4, 1 );

  connect( mHexInput, SIGNAL(dataChanged(const QByteArray &)),
	   this, SLOT(setData(const QByteArray &)) );
  connect( mDecInput, SIGNAL(dataChanged(const QByteArray &)),
	   this, SLOT(setData(const QByteArray &)) );
  connect( mOctInput, SIGNAL(dataChanged(const QByteArray &)),
	   this, SLOT(setData(const QByteArray &)) );
  connect( mBinInput, SIGNAL(dataChanged(const QByteArray &)),
	   this, SLOT(setData(const QByteArray &)) );
  connect( mTxtInput, SIGNAL(dataChanged(const QByteArray &)),
	   this, SLOT(setData(const QByteArray &)) );

}


CConverterDialog::~CConverterDialog( void )
{
}


void CConverterDialog::showEvent( QShowEvent *e )  
{
  KDialogBase::showEvent(e);
  mHexInput->setFocus();
}


void CConverterDialog::setData( const QByteArray &data )
{
  mHexInput->blockSignals(true);
  mDecInput->blockSignals(true);
  mOctInput->blockSignals(true);
  mBinInput->blockSignals(true);
  mTxtInput->blockSignals(true);
  mHexInput->setData(data);
  mDecInput->setData(data);
  mOctInput->setData(data);
  mBinInput->setData(data);
  mTxtInput->setData(data);
  mHexInput->blockSignals(false);
  mDecInput->blockSignals(false);
  mOctInput->blockSignals(false);
  mBinInput->blockSignals(false);
  mTxtInput->blockSignals(false);
}

void CConverterDialog::slotUser1( void ) // Clear
{
  QByteArray buf;
  setData( buf );
}

void CConverterDialog::slotUser2( void ) // On Cursor
{
  QByteArray buf;
  emit probeCursorValue( buf, 1 );
  setData( buf );
}


#include "converterdialog.moc"