Commit c325dba8 authored by Joachim Eibl's avatar Joachim Eibl
Browse files

From Alexey Kostromin: Change an encoding in diff text window via click on encoding label.


git-svn-id: https://kdiff3.svn.sourceforge.net/svnroot/kdiff3/trunk@145 c8aa9fed-2811-0410-8543-893ada326672
parent c91a0915
......@@ -234,6 +234,10 @@ void SourceData::setFileAccess( const FileAccess& fileAccess )
m_tempInputFileName = "";
}
}
void SourceData::setEncoding(QTextCodec* pEncoding)
{
m_pEncoding = pEncoding;
}
void SourceData::setData( const QString& data )
{
......
......@@ -256,6 +256,7 @@ public:
void setFilename(const QString& filename);
void setFileAccess( const FileAccess& fa );
void setEncoding(QTextCodec* pEncoding);
//FileAccess& getFileAccess();
QString getFilename();
void setAliasName(const QString& a);
......
......@@ -31,6 +31,7 @@
#include <QLayout>
#include <QTextCodec>
#include <QUrl>
#include <QMenu>
#include <klocale.h>
#include <kfiledialog.h>
......@@ -1660,9 +1661,10 @@ public:
QLabel* m_pLineEndStyle;
QWidget* m_pTopLineWidget;
int m_winIdx;
};
DiffTextWindowFrame::DiffTextWindowFrame( QWidget* pParent, QStatusBar* pStatusBar, OptionDialog* pOptionDialog, int winIdx )
DiffTextWindowFrame::DiffTextWindowFrame( QWidget* pParent, QStatusBar* pStatusBar, OptionDialog* pOptionDialog, int winIdx, SourceData* psd)
: QWidget( pParent )
{
d = new DiffTextWindowFrameData;
......@@ -1702,7 +1704,8 @@ DiffTextWindowFrame::DiffTextWindowFrame( QWidget* pParent, QStatusBar* pStatusB
pHL2->setMargin(0);
pHL2->setSpacing(2);
pHL2->addWidget( d->m_pTopLine, 0 );
d->m_pEncoding = new QLabel(i18n("Encoding:"));
d->m_pEncoding = new EncodingLabel(i18n("Encoding:"), this, psd, pOptionDialog);
d->m_pLineEndStyle = new QLabel(i18n("Line end style:"));
pHL2->addWidget(d->m_pEncoding);
pHL2->addWidget(d->m_pLineEndStyle);
......@@ -1823,6 +1826,7 @@ bool DiffTextWindowFrame::eventFilter( QObject* o, QEvent* e )
}
}
}
return false;
}
......@@ -1837,12 +1841,120 @@ void DiffTextWindowFrame::slotReturnPressed()
void DiffTextWindowFrame::slotBrowseButtonClicked()
{
QString current = d->m_pFileSelection->text();
QString current = d->m_pFileSelection->text();
KUrl newURL = KFileDialog::getOpenUrl( current, 0, this);
if ( !newURL.isEmpty() )
{
DiffTextWindow* pDTW = d->m_pDiffTextWindow;
emit fileNameChanged( newURL.url(), pDTW->d->m_winIdx );
}
}
void DiffTextWindowFrame::sendEncodingChangedSignal(QTextCodec* c)
{
emit encodingChanged(c);
}
EncodingLabel::EncodingLabel( const QString & text, DiffTextWindowFrame* pDiffTextWindowFrame, SourceData * pSD, OptionDialog* pOptionDialog)
: QLabel(text)
{
m_pDiffTextWindowFrame = pDiffTextWindowFrame;
m_pOptionDialog = pOptionDialog;
m_pSourceData = pSD;
m_pContextEncodingMenu = 0;
setMouseTracking(true);
}
void EncodingLabel::mouseMoveEvent(QMouseEvent *)
{
// When there is no data to display or it came from clipboard,
// we will be use UTF-8 only,
// in that case there is no possibility to change the encoding in the SourceData
// so, we should hide the HandCursor and display usual ArrowCursor
if (m_pSourceData->isFromBuffer()||m_pSourceData->isEmpty())
setCursor(QCursor(Qt::ArrowCursor));
else setCursor(QCursor(Qt::PointingHandCursor));
}
void EncodingLabel::mousePressEvent(QMouseEvent *)
{
if (!(m_pSourceData->isFromBuffer()||m_pSourceData->isEmpty()))
{
delete m_pContextEncodingMenu;
m_pContextEncodingMenu = new QMenu(this);
QMenu* pContextEncodingSubMenu = new QMenu(m_pContextEncodingMenu);
int currentTextCodecEnum = m_pSourceData->getEncoding()->mibEnum(); // the codec that will be checked in the context menu
QList<int> mibs = QTextCodec::availableMibs();
QList<int> codecEnumList;
// Adding "main" encodings
insertCodec( i18n("Unicode, 8 bit"), QTextCodec::codecForName("UTF-8"), codecEnumList, m_pContextEncodingMenu, currentTextCodecEnum);
insertCodec( "", QTextCodec::codecForName("System"), codecEnumList, m_pContextEncodingMenu, currentTextCodecEnum);
// Adding recent encodings
if (m_pOptionDialog!=0)
{
QStringList& recentEncodings = m_pOptionDialog->m_recentEncodings;
foreach(QString s, recentEncodings)
{
insertCodec("", QTextCodec::codecForName(s.toAscii()), codecEnumList, m_pContextEncodingMenu, currentTextCodecEnum);
}
}
// Submenu to add the rest of available encodings
pContextEncodingSubMenu->setTitle(i18n("Other"));
foreach(int i, mibs)
{
QTextCodec* c = QTextCodec::codecForMib(i);
if ( c!=0 )
insertCodec("", c, codecEnumList, pContextEncodingSubMenu, currentTextCodecEnum);
}
m_pContextEncodingMenu->addMenu(pContextEncodingSubMenu);
m_pContextEncodingMenu->exec(QCursor::pos());
}
}
void EncodingLabel::insertCodec( const QString& visibleCodecName, QTextCodec* pCodec, QList<int> &codecEnumList, QMenu* pMenu, int currentTextCodecEnum)
{
int CodecMIBEnum = pCodec->mibEnum();
if ( pCodec!=0 && !codecEnumList.contains(CodecMIBEnum) )
{
QAction* pAction = new QAction( pMenu ); // menu takes ownership, so deleting the menu deletes the action too.
pAction->setText( visibleCodecName.isEmpty() ? QString(pCodec->name()) : visibleCodecName+" ("+pCodec->name()+")" );
pAction->setData(CodecMIBEnum);
pAction->setCheckable(true);
if (currentTextCodecEnum==CodecMIBEnum)
pAction->setChecked(true);
pMenu->addAction(pAction);
connect(pAction, SIGNAL(triggered()), this, SLOT(slotEncodingChanged()));
codecEnumList.append(CodecMIBEnum);
}
}
KUrl newURL = KFileDialog::getOpenUrl( current, 0, this);
if ( !newURL.isEmpty() )
void EncodingLabel::slotEncodingChanged()
{
QAction *pAction = qobject_cast<QAction *>(sender());
if (pAction)
{
DiffTextWindow* pDTW = d->m_pDiffTextWindow;
emit fileNameChanged( newURL.url(), pDTW->d->m_winIdx );
QTextCodec * pCodec = QTextCodec::codecForMib(pAction->data().toInt());
if (pCodec!=0)
{
QString s( pCodec->name() );
QStringList& recentEncodings = m_pOptionDialog->m_recentEncodings;
if ( !recentEncodings.contains(s) && s!="UTF-8" && s!="System" )
{
int itemsToRemove = recentEncodings.size() - m_maxRecentEncodings + 1;
for (int i=0; i<itemsToRemove; ++i)
{
recentEncodings.removeFirst();
}
recentEncodings.append(s);
}
}
m_pDiffTextWindowFrame->sendEncodingChangedSignal(pCodec);
}
}
......@@ -22,10 +22,12 @@
#include <QWidget>
class QMenu;
class QStatusBar;
class OptionDialog;
class DiffTextWindowData;
class DiffTextWindowFrame;
class EncodingLabel;
class DiffTextWindow : public QWidget
{
......@@ -35,6 +37,7 @@ public:
DiffTextWindowFrame* pParent,
QStatusBar* pStatusBar,
OptionDialog* pOptionDialog,
int winIdx
);
~DiffTextWindow();
......@@ -116,13 +119,15 @@ class DiffTextWindowFrame : public QWidget
{
Q_OBJECT
public:
DiffTextWindowFrame( QWidget* pParent, QStatusBar* pStatusBar, OptionDialog* pOptionDialog, int winIdx );
DiffTextWindowFrame( QWidget* pParent, QStatusBar* pStatusBar, OptionDialog* pOptionDialog, int winIdx, SourceData* psd);
~DiffTextWindowFrame();
DiffTextWindow* getDiffTextWindow();
void init();
void setFirstLine(int firstLine);
void sendEncodingChangedSignal(QTextCodec* c);
signals:
void fileNameChanged(const QString&, int);
void encodingChanged(QTextCodec*);
protected:
bool eventFilter( QObject*, QEvent* );
//void paintEvent(QPaintEvent*);
......@@ -133,6 +138,25 @@ private:
DiffTextWindowFrameData* d;
};
class EncodingLabel : public QLabel
{
Q_OBJECT
public:
EncodingLabel( const QString & text, DiffTextWindowFrame* pDiffTextWindowFrame, SourceData* psd, OptionDialog* pOptionDialog);
protected:
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
private slots:
void slotEncodingChanged();
private:
DiffTextWindowFrame* m_pDiffTextWindowFrame; //To send "EncodingChanged" signal
QMenu* m_pContextEncodingMenu;
SourceData* m_pSourceData; //SourceData to get access to "isEmpty()" and "isFromBuffer()" functions
static const int m_maxRecentEncodings = 5;
OptionDialog* m_pOptionDialog;
void insertCodec( const QString& visibleCodecName, QTextCodec* pCodec, QList<int> &CodecEnumList, QMenu* pMenu, int currentTextCodecEnum);
};
#endif
......@@ -302,8 +302,7 @@ class KDiff3App : public QSplitter
OptionDialog* m_pOptionDialog;
FindDialog* m_pFindDialog;
void init( bool bAuto=false, TotalDiffStatus* pTotalDiffStatus=0, bool bLoadFiles=true );
void init( bool bAuto=false, TotalDiffStatus* pTotalDiffStatus=0, bool bLoadFiles=true, bool bUseCurrentEncoding = false);
virtual bool eventFilter( QObject* o, QEvent* e );
virtual void resizeEvent(QResizeEvent*);
......@@ -399,8 +398,10 @@ public slots:
void slotJoinDiffs();
void slotAddManualDiffHelp();
void slotClearManualDiffHelpList();
void slotNoRelevantChangesDetected();
void slotEncodingChangedA(QTextCodec*);
void slotEncodingChangedB(QTextCodec*);
void slotEncodingChangedC(QTextCodec*);
};
#endif // KDIFF3_H
......@@ -523,6 +523,8 @@ void OptionDialog::setupOtherOptions()
new OptionStringList( "RecentBFiles", &m_recentBFiles, this );
new OptionStringList( "RecentCFiles", &m_recentCFiles, this );
new OptionStringList( "RecentOutputFiles", &m_recentOutputFiles, this );
new OptionStringList( "RecentEncodings", &m_recentEncodings, this );
}
void OptionDialog::setupFontPage( void )
......
......@@ -132,6 +132,8 @@ public:
QStringList m_recentBFiles;
QStringList m_recentCFiles;
QStringList m_recentEncodings;
QStringList m_recentOutputFiles;
// Directory Merge options
......
......@@ -237,9 +237,10 @@ bool KDiff3App::runDiff( const LineData* p1, int size1, const LineData* p2, int
return true;
}
void KDiff3App::init( bool bAuto, TotalDiffStatus* pTotalDiffStatus, bool bLoadFiles )
void KDiff3App::init( bool bAuto, TotalDiffStatus* pTotalDiffStatus, bool bLoadFiles, bool bUseCurrentEncoding)
{
ProgressProxy pp;
// When doing a full analysis in the directory-comparison, then the statistics-results
// will be stored in the given TotalDiffStatus. Otherwise it will be 0.
bool bGUI = pTotalDiffStatus == 0;
......@@ -298,11 +299,17 @@ void KDiff3App::init( bool bAuto, TotalDiffStatus* pTotalDiffStatus, bool bLoadF
// First get all input data.
pp.setInformation(i18n("Loading A"));
m_sd1.readAndPreprocess(m_pOptionDialog->m_pEncodingA, m_pOptionDialog->m_bAutoDetectUnicodeA );
if (bUseCurrentEncoding==true)
m_sd1.readAndPreprocess(m_sd1.getEncoding(), false);
else
m_sd1.readAndPreprocess(m_pOptionDialog->m_pEncodingA, m_pOptionDialog->m_bAutoDetectUnicodeA);
pp.step();
pp.setInformation(i18n("Loading B"));
m_sd2.readAndPreprocess(m_pOptionDialog->m_pEncodingB, m_pOptionDialog->m_bAutoDetectUnicodeB );
if (bUseCurrentEncoding==true)
m_sd2.readAndPreprocess(m_sd2.getEncoding(), false);
else
m_sd2.readAndPreprocess(m_pOptionDialog->m_pEncodingB, m_pOptionDialog->m_bAutoDetectUnicodeB);
pp.step();
}
else
......@@ -336,7 +343,10 @@ void KDiff3App::init( bool bAuto, TotalDiffStatus* pTotalDiffStatus, bool bLoadF
if (bLoadFiles)
{
pp.setInformation(i18n("Loading C"));
m_sd3.readAndPreprocess(m_pOptionDialog->m_pEncodingC, m_pOptionDialog->m_bAutoDetectUnicodeC );
if (bUseCurrentEncoding==true)
m_sd3.readAndPreprocess(m_sd3.getEncoding(), false);
else
m_sd3.readAndPreprocess(m_pOptionDialog->m_pEncodingC, m_pOptionDialog->m_bAutoDetectUnicodeC);
pp.step();
}
......@@ -742,13 +752,13 @@ void KDiff3App::initView()
m_pDiffVScrollBar = new QScrollBar( Qt::Vertical, pDiffWindowFrame );
pDiffHLayout->addWidget( m_pDiffVScrollBar );
m_pDiffTextWindowFrame1 = new DiffTextWindowFrame( m_pDiffWindowSplitter, statusBar(), m_pOptionDialog, 1 );
m_pDiffTextWindowFrame1 = new DiffTextWindowFrame( m_pDiffWindowSplitter, statusBar(), m_pOptionDialog, 1, &m_sd1);
m_pDiffWindowSplitter->addWidget(m_pDiffTextWindowFrame1);
//m_pDiffTextWindowFrame1->show();
m_pDiffTextWindowFrame2 = new DiffTextWindowFrame( m_pDiffWindowSplitter, statusBar(), m_pOptionDialog, 2 );
m_pDiffTextWindowFrame2 = new DiffTextWindowFrame( m_pDiffWindowSplitter, statusBar(), m_pOptionDialog, 2, &m_sd2);
m_pDiffWindowSplitter->addWidget(m_pDiffTextWindowFrame2);
//m_pDiffTextWindowFrame2->show();
m_pDiffTextWindowFrame3 = new DiffTextWindowFrame( m_pDiffWindowSplitter, statusBar(), m_pOptionDialog, 3 );
m_pDiffTextWindowFrame3 = new DiffTextWindowFrame( m_pDiffWindowSplitter, statusBar(), m_pOptionDialog, 3, &m_sd3);
m_pDiffWindowSplitter->addWidget(m_pDiffTextWindowFrame3);
m_pDiffTextWindow1 = m_pDiffTextWindowFrame1->getDiffTextWindow();
m_pDiffTextWindow2 = m_pDiffTextWindowFrame2->getDiffTextWindow();
......@@ -757,6 +767,10 @@ void KDiff3App::initView()
connect(m_pDiffTextWindowFrame2, SIGNAL(fileNameChanged(const QString&,int)), this, SLOT(slotFileNameChanged(const QString&,int)));
connect(m_pDiffTextWindowFrame3, SIGNAL(fileNameChanged(const QString&,int)), this, SLOT(slotFileNameChanged(const QString&,int)));
connect(m_pDiffTextWindowFrame1, SIGNAL(encodingChanged(QTextCodec*)), this, SLOT(slotEncodingChangedA(QTextCodec*)));
connect(m_pDiffTextWindowFrame2, SIGNAL(encodingChanged(QTextCodec*)), this, SLOT(slotEncodingChangedB(QTextCodec*)));
connect(m_pDiffTextWindowFrame3, SIGNAL(encodingChanged(QTextCodec*)), this, SLOT(slotEncodingChangedC(QTextCodec*)));
// Merge window
m_pMergeWindowFrame = new QWidget( pVSplitter );
m_pMergeWindowFrame->setObjectName("MergeWindowFrame");
......@@ -1704,6 +1718,8 @@ void KDiff3App::slotShowWhiteSpaceToggled()
{
m_pOptionDialog->m_bShowWhiteSpaceCharacters = showWhiteSpaceCharacters->isChecked();
m_pOptionDialog->m_bShowWhiteSpace = showWhiteSpace->isChecked();
showWhiteSpaceCharacters->setEnabled( showWhiteSpace->isChecked() );
if ( m_pDiffTextWindow1!=0 )
m_pDiffTextWindow1->update();
......@@ -1726,6 +1742,7 @@ void KDiff3App::slotShowLineNumbersToggled()
m_pDiffTextWindow1->update();
if ( m_pDiffTextWindow2!=0 )
m_pDiffTextWindow2->update();
if ( m_pDiffTextWindow3!=0 )
m_pDiffTextWindow3->update();
}
......@@ -2280,6 +2297,27 @@ void KDiff3App::slotClearManualDiffHelpList()
slotRefresh();
}
void KDiff3App::slotEncodingChangedA(QTextCodec* c)
{
m_sd1.setEncoding(c);
init( false, 0, true, true); // Init with reload
slotRefresh();
}
void KDiff3App::slotEncodingChangedB(QTextCodec* c)
{
m_sd2.setEncoding(c);
init( false, 0, true, true); // Init with reload
slotRefresh();
}
void KDiff3App::slotEncodingChangedC(QTextCodec* c)
{
m_sd3.setEncoding(c);
init( false, 0, true, true ); // Init with reload
slotRefresh();
}
void KDiff3App::slotUpdateAvailabilities()
{
if (m_pMainSplitter==0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment