Commit 347479b5 authored by Michael Reeves's avatar Michael Reeves

Update QT API usage.

parent c6c68720
/**
* Copyright (C) 2018 Michael Reeves reeves.87@gmail.com
*
*
* This file is part of KDiff3.
*
*
* KDiff3 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.
*
*
* KDiff3 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 KDiff3. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef UTILS_H
#define UTILS_H
#include <QFontMetrics>
#include <QStringList>
#include <QString>
......@@ -28,6 +29,27 @@ class Utils{
public:
static bool wildcardMultiMatch(const QString& wildcard, const QString& testString, bool bCaseSensitive);
static QString getArguments(QString cmd, QString& program, QStringList& args);
//Where posiable use QTextLayout in place of these functions especially when dealing with non-latin scripts.
inline static int getHorizontalAdvance(const QFontMetrics &metrics, const QString& s)
{
//Warning: The Qt API used here is not accurate for some non-latin characters.
#if QT_VERSION < QT_VERSION_CHECK(5,12,0)
return metrics.width(s);
#else
return metrics.horizontalAdvance(s);
#endif
}
inline static int getHorizontalAdvance(const QFontMetrics &metrics, const QChar& c)
{
//Warning: The Qt API used here is not accurate for some non-latin characters.
#if QT_VERSION < QT_VERSION_CHECK(5,12,0)
return metrics.width(s);
#else
return metrics.horizontalAdvance(c);
#endif
}
};
#endif
......@@ -18,6 +18,7 @@
#include "options.h"
#include "FileNameLineEdit.h"
#include "RLPainter.h"
#include "Utils.h"
#include <algorithm>
#include <cmath>
......@@ -288,7 +289,7 @@ int DiffTextWindow::getFirstLine()
void DiffTextWindow::setHorizScrollOffset(int horizScrollOffset)
{
int fontWidth = fontMetrics().width('0');
int fontWidth = Utils::getHorizontalAdvance(fontMetrics(), '0');
int xOffset = d->leftInfoWidth() * fontWidth;
int deltaX = d->m_horizScrollOffset - std::max(0, horizScrollOffset);
......@@ -433,7 +434,7 @@ void DiffTextWindow::mousePressEvent(QMouseEvent* e)
int pos;
convertToLinePos(e->x(), e->y(), line, pos);
int fontWidth = fontMetrics().width('0');
int fontWidth = Utils::getHorizontalAdvance(fontMetrics(), '0');
int xOffset = d->leftInfoWidth() * fontWidth;
if((!d->m_pOptions->m_bRightToLeftLanguage && e->x() < xOffset) || (d->m_pOptions->m_bRightToLeftLanguage && e->x() > width() - xOffset))
......@@ -563,7 +564,7 @@ void DiffTextWindow::mouseMoveEvent(QMouseEvent* e)
// Scroll because mouse moved out of the window
const QFontMetrics& fm = fontMetrics();
int fontWidth = fm.width('0');
int fontWidth = Utils::getHorizontalAdvance(fm, '0');
int deltaX = 0;
int deltaY = 0;
if(!d->m_pOptions->m_bRightToLeftLanguage)
......@@ -756,7 +757,7 @@ void DiffTextWindowData::prepareTextLayout(QTextLayout& textLayout, bool /*bFirs
int leading = m_pDiffTextWindow->fontMetrics().leading();
int height = 0;
int fontWidth = m_pDiffTextWindow->fontMetrics().width('0');
int fontWidth = Utils::getHorizontalAdvance(m_pDiffTextWindow->fontMetrics(), '0');
int xOffset = leftInfoWidth() * fontWidth - m_horizScrollOffset;
int textWidth = visibleTextWidth;
if(textWidth < 0)
......@@ -813,7 +814,7 @@ void DiffTextWindowData::writeLine(
const QFontMetrics& fm = p.fontMetrics();
int fontHeight = fm.lineSpacing();
int fontAscent = fm.ascent();
int fontWidth = fm.width('0');
int fontWidth = Utils::getHorizontalAdvance(fm, '0');
int xOffset = leftInfoWidth() * fontWidth - m_horizScrollOffset;
int yOffset = (line - m_firstLine) * fontHeight;
......@@ -1027,7 +1028,7 @@ void DiffTextWindow::paintEvent(QPaintEvent* e)
int endLine = std::min(d->m_firstLine + getNofVisibleLines() + 2, getNofLines());
RLPainter p(this, d->m_pOptions->m_bRightToLeftLanguage, width(), fontMetrics().width('0'));
RLPainter p(this, d->m_pOptions->m_bRightToLeftLanguage, width(), Utils::getHorizontalAdvance(fontMetrics(), '0'));
p.setFont(font());
p.QPainter::fillRect(invalidRect, d->m_pOptions->m_bgColor);
......@@ -1182,7 +1183,7 @@ void DiffTextWindow::resizeEvent(QResizeEvent* e)
QSize s = e->size();
QFontMetrics fm = fontMetrics();
int visibleLines = s.height() / fm.lineSpacing() - 2;
int visibleColumns = s.width() / fm.width('0') - d->leftInfoWidth();
int visibleColumns = s.width() / Utils::getHorizontalAdvance(fm, '0') - d->leftInfoWidth();
if(e->size().height() != e->oldSize().height())
emit resizeHeightChangedSignal(visibleLines);
if(e->size().width() != e->oldSize().width())
......@@ -1201,7 +1202,7 @@ int DiffTextWindow::getNofVisibleLines()
int DiffTextWindow::getVisibleTextAreaWidth()
{
QFontMetrics fm = fontMetrics();
return width() - d->leftInfoWidth() * fm.width('0');
return width() - d->leftInfoWidth() * Utils::getHorizontalAdvance(fm, '0');
}
QString DiffTextWindow::getSelection()
......@@ -1576,7 +1577,7 @@ void DiffTextWindow::recalcWordWrapHelper(int wrapLineVectorSize, int visibleTex
if(visibleTextWidth < 0)
visibleTextWidth = getVisibleTextAreaWidth();
else
visibleTextWidth -= d->leftInfoWidth() * fontMetrics().width('0');
visibleTextWidth -= d->leftInfoWidth() * Utils::getHorizontalAdvance(fontMetrics(), '0');
int i;
int wrapLineIdx = 0;
int size = d->m_pDiff3LineVector->size();
......@@ -1837,8 +1838,7 @@ void DiffTextWindowFrame::setFirstLine(int firstLine)
int l = pDTW->calcTopLineInFile(firstLine);
int w = d->m_pTopLine->fontMetrics().width(
s + ' ' + QString().fill('0', lineNumberWidth));
int w = Utils::getHorizontalAdvance(d->m_pTopLine->fontMetrics(), s + ' ' + QString().fill('0', lineNumberWidth));
d->m_pTopLine->setMinimumWidth(w);
if(l == -1)
......
......@@ -597,13 +597,14 @@ class DirectoryMergeWindow::DirMergeItemDelegate : public QStyledItemDelegate
thePainter->drawRect(x + 1, y + yOffset - 1, icon.width() + 2, icon.height() + 2);
thePainter->setPen(Qt::white);
QString s(QChar('A' + i - 1));
thePainter->drawText(x + 2 + (icon.width() - thePainter->fontMetrics().width(s)) / 2,
thePainter->drawText(x + 2 + (icon.width() - Utils::getHorizontalAdvance(thePainter->fontMetrics(), s)) / 2,
y + yOffset + (icon.height() + thePainter->fontMetrics().ascent()) / 2 - 1,
s);
}
else
{
thePainter->setPen(m_pDMW->palette().background().color());
thePainter->setPen(m_pDMW->palette().window().color());
thePainter->drawRect(x + 1, y + yOffset - 1, icon.width() + 2, icon.height() + 2);
}
return;
......
......@@ -132,7 +132,7 @@ void FileAccess::loadData()
m_fileInfo.setCaching(true);
if(parent() == nullptr)
m_baseDir = m_fileInfo.absoluteFilePath();
m_baseDir.setPath(m_fileInfo.absoluteFilePath());
else
m_baseDir = m_pParent->m_baseDir;
......
......@@ -23,6 +23,7 @@
#include "difftextwindow.h"
#include "mergeresultwindow.h"
#include "RLPainter.h"
#include "Utils.h"
#ifndef Q_OS_WIN
#include <unistd.h>
......@@ -816,7 +817,7 @@ void printDiffTextWindow(RLPainter& painter, const QRect& view, const QString& h
painter.translate(0, view.top());
pDiffTextWindow->print(painter, view, line, linesPerPage);
painter.resetMatrix();
painter.resetTransform();
}
void KDiff3App::slotFilePrint()
......@@ -863,7 +864,7 @@ void KDiff3App::slotFilePrint()
{
slotStatusMsg(i18n("Printing..."));
// create a painter to paint on the printer object
RLPainter painter(&printer, m_pOptions->m_bRightToLeftLanguage, width(), fontMetrics().width('W'));
RLPainter painter(&printer, m_pOptions->m_bRightToLeftLanguage, width(), Utils::getHorizontalAdvance(fontMetrics(),'W'));
QPaintDevice* pPaintDevice = painter.device();
int dpiy = pPaintDevice->logicalDpiY();
......@@ -880,7 +881,7 @@ void KDiff3App::slotFilePrint()
QString topLineText = i18n("Top line");
//int headerWidth = fm.width( m_sd1.getAliasName() + ", "+topLineText+": 01234567" );
int headerLines = fm.width(m_sd1.getAliasName() + ", " + topLineText + ": 01234567") / columnWidth + 1;
int headerLines = Utils::getHorizontalAdvance(fm, m_sd1.getAliasName() + ", " + topLineText + ": 01234567") / columnWidth + 1;
int headerMargin = headerLines * fm.height() + 3; // Text + one horizontal line
int footerMargin = fm.height() + 3;
......@@ -1005,7 +1006,7 @@ void KDiff3App::slotFilePrint()
QString s = bPrintCurrentPage ? QString("")
: QString::number(page) + '/' + QString::number(totalNofPages);
if(bPrintSelection) s += i18n(" (Selection)");
painter.drawText((view.right() - painter.fontMetrics().width(s)) / 2,
painter.drawText((view.right() - Utils::getHorizontalAdvance(painter.fontMetrics(), s)) / 2,
view.bottom() + painter.fontMetrics().ascent() + 5, s);
bFirstPrintedPage = true;
......
......@@ -13,6 +13,7 @@
#include "options.h"
#include "RLPainter.h"
#include "guiutils.h"
#include "Utils.h"
#include <QApplication>
#include <QClipboard>
......@@ -667,7 +668,7 @@ int MergeResultWindow::getNofVisibleLines()
int MergeResultWindow::getTextXOffset()
{
QFontMetrics fm = fontMetrics();
return 3 * fm.width('0');
return 3 * Utils::getHorizontalAdvance(fm, '0');
}
void MergeResultWindow::resizeEvent(QResizeEvent* e)
......@@ -1914,7 +1915,7 @@ void MergeResultWindow::writeLine(
else
Q_ASSERT(true);
xOffset -= fm.width('0');
xOffset -= Utils::getHorizontalAdvance(fm, '0');
p.setPen(m_pOptions->m_fgColor);
if(rangeMark & 1) // begin mark
{
......@@ -1964,7 +1965,7 @@ void MergeResultWindow::paintEvent(QPaintEvent*)
bool bOldSelectionContainsData = m_selection.selectionContainsData();
const QFontMetrics& fm = fontMetrics();
int fontWidth = fm.width('0');
int fontWidth = Utils::getHorizontalAdvance(fm, '0');
if(!m_bCursorUpdate) // Don't redraw everything for blinking cursor?
{
......@@ -2255,7 +2256,7 @@ void MergeResultWindow::mouseMoveEvent(QMouseEvent* e)
// Scroll because mouse moved out of the window
const QFontMetrics& fm = fontMetrics();
int fontWidth = fm.width('0');
int fontWidth = Utils::getHorizontalAdvance(fm, '0');
int topLineYOffset = 0;
int deltaX = 0;
int deltaY = 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