• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • charsetconversion
charsetconversiontool.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library 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 GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "charsetconversiontool.h"
24 
25 // tool
26 #include "charsetconversionjob.h"
27 // Okteta Kasten gui
28 #include <bytearrayview.h>
29 // Okteta Kasten core
30 #include <bytearraydocument.h>
31 // Okteta core
32 #include <charcodec.h>
33 #include <abstractbytearraymodel.h>
34 #include <changesdescribable.h>
35 // KDE
36 #include <KLocale>
37 // Qt
38 #include <QtGui/QApplication>
39 
40 
41 namespace Kasten2
42 {
43 
44 CharsetConversionTool::CharsetConversionTool()
45  : mConversionDirection( ConvertFrom ),
46  mSubstitutingMissingChars(false),
47  mSubstituteByte( 0 ),
48  mByteArrayView( 0 ),
49  mByteArrayModel( 0 )
50 {
51  setObjectName( QLatin1String("CharsetConversion") );
52 }
53 
54 bool CharsetConversionTool::isApplyable() const
55 {
56  return ( mByteArrayModel &&
57  mByteArrayView && mByteArrayView->hasSelectedData() &&
58  ! mOtherCharCodecName.isEmpty() &&
59  mByteArrayView->charCodingName() != mOtherCharCodecName );
60 }
61 
62 QString CharsetConversionTool::title() const
63 {
64  return i18nc("@title:window of the tool to convert between charsets",
65  "Charset Conversion");
66 }
67 
68 QString CharsetConversionTool::otherCharCodecName() const
69 {
70  return mOtherCharCodecName;
71 }
72 
73 CharsetConversionTool::ConversionDirection CharsetConversionTool::conversionDirection() const
74 {
75  return mConversionDirection;
76 }
77 
78 bool CharsetConversionTool::isSubstitutingMissingChars() const
79 {
80  return mSubstitutingMissingChars;
81 }
82 
83 Okteta::Byte CharsetConversionTool::substituteByte() const
84 {
85  return mSubstituteByte;
86 }
87 
88 
89 void CharsetConversionTool::setTargetModel( AbstractModel* model )
90 {
91  if( mByteArrayView ) mByteArrayView->disconnect( this );
92 
93  mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : 0;
94 
95  ByteArrayDocument* document =
96  mByteArrayView ? qobject_cast<ByteArrayDocument*>( mByteArrayView->baseModel() ) : 0;
97  mByteArrayModel = document ? document->content() : 0;
98 
99  if( mByteArrayView && mByteArrayModel )
100  {
101  connect( mByteArrayView, SIGNAL(charCodecChanged(QString)),
102  SLOT(onViewChanged()) );
103  connect( mByteArrayView, SIGNAL(selectedDataChanged(const Kasten2::AbstractModelSelection*)),
104  SLOT(onViewChanged()) );
105  }
106 
107  onViewChanged();
108 }
109 
110 void CharsetConversionTool::setOtherCharCodecName( const QString& codecName )
111 {
112  if( codecName == mOtherCharCodecName )
113  return;
114 
115  mOtherCharCodecName = codecName;
116  emit isApplyableChanged( isApplyable() );
117 }
118 
119 void CharsetConversionTool::setConversionDirection( int conversionDirection )
120 {
121  mConversionDirection = (ConversionDirection)conversionDirection;
122 }
123 
124 void CharsetConversionTool::setSubstitutingMissingChars( bool isSubstitutingMissingChars )
125 {
126  mSubstitutingMissingChars = isSubstitutingMissingChars;
127 }
128 
129 void CharsetConversionTool::setSubstituteByte( int byte )
130 {
131  mSubstituteByte = (Okteta::Byte)byte;
132 }
133 
134 
135 void CharsetConversionTool::convertChars()
136 {
137  QApplication::setOverrideCursor( Qt::WaitCursor );
138 
139  const Okteta::AddressRange convertedSection = mByteArrayView->selection();
140  QByteArray conversionResult;
141  conversionResult.resize( convertedSection.width() );
142 
143  Okteta::CharCodec* viewCharCodec =
144  Okteta::CharCodec::createCodec( mByteArrayView->charCodingName() );
145  Okteta::CharCodec* otherCharCodec =
146  Okteta::CharCodec::createCodec( mOtherCharCodecName );
147  const bool convertToOther = (mConversionDirection == ConvertTo);
148  Okteta::CharCodec* fromCharCodec = convertToOther ? viewCharCodec : otherCharCodec;
149  Okteta::CharCodec* toCharCodec = convertToOther ? otherCharCodec : viewCharCodec;
150  CharsetConversionJob* charsetConversionJob =
151  new CharsetConversionJob( reinterpret_cast<Okteta::Byte*>(conversionResult.data()),
152  mByteArrayModel, convertedSection,
153  convertToOther ? viewCharCodec : otherCharCodec,
154  convertToOther ? otherCharCodec : viewCharCodec,
155  mSubstitutingMissingChars, mSubstituteByte
156  ); // TODO: report also actually converted bytes
157  const bool success = charsetConversionJob->exec();
158 
159  if( success ) //TODO: if nothing needed to be converted, just report and don't add change
160  {
161  Okteta::ChangesDescribable *changesDescribable =
162  qobject_cast<Okteta::ChangesDescribable*>( mByteArrayModel );
163 
164  if( changesDescribable )
165  {
166  const QString description =
167  i18nc("Converted from charset 1 to charset 2",
168  "%1 to %2", fromCharCodec->name(), toCharCodec->name());
169  changesDescribable->openGroupedChange( description );
170  }
171  mByteArrayModel->replace( convertedSection, conversionResult );
172  if( changesDescribable )
173  changesDescribable->closeGroupedChange();
174  }
175 
176  delete viewCharCodec;
177  delete otherCharCodec;
178 
179  QApplication::restoreOverrideCursor();
180 
181  const QMap<Okteta::Byte, int>& failedPerByteCount = charsetConversionJob->failedPerByteCount();
182  const int convertedBytesCount = charsetConversionJob->convertedBytesCount();
183 
184  mByteArrayView->setFocus();
185 
186  emit conversionDone( success, convertedBytesCount, failedPerByteCount );
187 }
188 
189 
190 void CharsetConversionTool::onViewChanged()
191 {
192  emit isApplyableChanged( isApplyable() );
193 }
194 
195 CharsetConversionTool::~CharsetConversionTool()
196 {
197 }
198 
199 }
Kasten2::ByteArrayView::setFocus
virtual void setFocus()
Definition: bytearrayview.cpp:155
Kasten2::CharsetConversionTool::title
virtual QString title() const
Definition: charsetconversiontool.cpp:62
Okteta::AbstractByteArrayModel::replace
virtual Size replace(const AddressRange &removeRange, const Byte *insertData, int insertLength)=0
replaces as much as possible
charsetconversiontool.h
abstractbytearraymodel.h
Kasten2::CharsetConversionTool::conversionDone
void conversionDone(bool success, int convertedBytesCount, const QMap< Okteta::Byte, int > &failedPerByteCount)
Kasten2::CharsetConversionTool::isSubstitutingMissingChars
bool isSubstitutingMissingChars() const
Definition: charsetconversiontool.cpp:78
Okteta::ChangesDescribable::closeGroupedChange
virtual void closeGroupedChange(const QString &description=QString())=0
Kasten2::ByteArrayView::charCodingName
QString charCodingName() const
Definition: bytearrayview.cpp:255
Kasten2::CharsetConversionTool::isApplyable
bool isApplyable() const
Definition: charsetconversiontool.cpp:54
KDE::NumberRange< Address, Size >
charsetconversionjob.h
Okteta::ChangesDescribable::openGroupedChange
virtual void openGroupedChange(const QString &description=QString())=0
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::ChangesDescribable
Definition: changesdescribable.h:33
Kasten2::CharsetConversionJob
Definition: charsetconversionjob.h:42
Kasten2::CharsetConversionTool::setOtherCharCodecName
void setOtherCharCodecName(const QString &codecName)
Definition: charsetconversiontool.cpp:110
Kasten2::CharsetConversionTool::isApplyableChanged
void isApplyableChanged(bool isApplyable)
Kasten2::CharsetConversionJob::failedPerByteCount
const QMap< Okteta::Byte, int > & failedPerByteCount() const
Definition: charsetconversionjob.h:97
Kasten2::ByteArrayView::hasSelectedData
virtual bool hasSelectedData() const
Definition: bytearrayview.cpp:178
Kasten2::AbstractModel::baseModel
AbstractModel * baseModel() const
Definition: abstractmodel.cpp:40
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
Kasten2::ByteArrayView::selection
Okteta::AddressRange selection() const
Definition: bytearrayview.cpp:271
Kasten2::CharsetConversionTool::setConversionDirection
void setConversionDirection(int conversionDirection)
Definition: charsetconversiontool.cpp:119
Okteta::CharCodec
Definition: charcodec.h:42
Kasten2::CharsetConversionTool::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: charsetconversiontool.cpp:89
Okteta::CharCodec::createCodec
static CharCodec * createCodec(CharCoding charCoding)
Definition: charcodec.cpp:68
Kasten2::CharsetConversionTool::CharsetConversionTool
CharsetConversionTool()
Definition: charsetconversiontool.cpp:44
Kasten2::CharsetConversionJob::exec
bool exec()
Definition: charsetconversionjob.cpp:38
Kasten2::CharsetConversionTool::setSubstituteByte
void setSubstituteByte(int byte)
Definition: charsetconversiontool.cpp:129
Kasten2::AbstractModelSelection
Definition: abstractmodelselection.h:35
Kasten2::CharsetConversionTool::otherCharCodecName
QString otherCharCodecName() const
Definition: charsetconversiontool.cpp:68
Kasten2::AbstractModel::findBaseModel
T findBaseModel() const
returns the first baseModel which is of type T, or null if none is found.
Definition: abstractmodel.h:93
charcodec.h
Kasten2::CharsetConversionTool::setSubstitutingMissingChars
void setSubstitutingMissingChars(bool isSubstitutingMissingChars)
Definition: charsetconversiontool.cpp:124
changesdescribable.h
Kasten2::CharsetConversionTool::~CharsetConversionTool
virtual ~CharsetConversionTool()
Definition: charsetconversiontool.cpp:195
Okteta::CharCodec::name
virtual const QString & name() const =0
Kasten2::CharsetConversionTool::conversionDirection
ConversionDirection conversionDirection() const
Definition: charsetconversiontool.cpp:73
Kasten2::CharsetConversionJob::convertedBytesCount
int convertedBytesCount() const
Definition: charsetconversionjob.h:95
Kasten2::CharsetConversionTool::ConvertTo
Definition: charsetconversiontool.h:56
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::CharsetConversionTool::ConversionDirection
ConversionDirection
Definition: charsetconversiontool.h:56
bytearraydocument.h
Kasten2::CharsetConversionTool::convertChars
void convertChars()
Definition: charsetconversiontool.cpp:135
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::CharsetConversionTool::substituteByte
Okteta::Byte substituteByte() const
Definition: charsetconversiontool.cpp:83
QMap< Okteta::Byte, int >
bytearrayview.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal