From 8d683548b138a3b24b63f77d36859abfa1058d01 Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Tue, 30 Oct 2007 19:20:27 +0100 Subject: [PATCH] Removed dependency to QtGui from diff/* files. Now all painting is done in qt/* files. diff/* files are only library for storing data. --- src/Makefile | 6 +- src/diff/diff.cpp | 50 ++-------------- src/diff/diff.h | 13 ++-- src/diff/file.cpp | 57 ------------------ src/diff/file.h | 9 +-- src/diff/hunk.cpp | 47 --------------- src/diff/hunk.h | 5 -- src/diff/snippet.cpp | 22 ------- src/diff/snippet.h | 17 +----- src/diff/text.cpp | 50 ---------------- src/diff/text.h | 5 -- src/diff/utils.h | 22 +++---- src/qt/diff_view.cpp | 158 ++++++++++++++++++++++++++++++++++++++++++++----- src/qt/diff_view.h | 18 ++++++ src/qt/main_window.cpp | 3 +- 15 files changed, 193 insertions(+), 289 deletions(-) rewrite src/diff/diff.cpp (95%) delete mode 100644 src/diff/file.cpp delete mode 100644 src/diff/hunk.cpp delete mode 100644 src/diff/text.cpp diff --git a/src/Makefile b/src/Makefile index 6a5ba24..88e4db4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,7 +13,7 @@ QT_LIBS = -lQtGui -L/opt/qt4-debug/lib -L/usr/X11R6/lib \ -lgthread-2.0 -lrt -lglib-2.0 -lpthread -lXi -lXrender -lXrandr -lXfixes \ -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lm -ldl -ALL_FLAGS = $(CXXFLAGS) $(DEBUGFLAGS) $(QT_INCLUDE) +ALL_FLAGS = $(CXXFLAGS) $(DEBUGFLAGS) $(QT_INCLUDE) -Lobjs ALL_LD_FLAGS = $(QT_LIBS) @@ -21,7 +21,7 @@ VPATH = diff qt parser TARGETS = qshowdiff -DIFF_OBJS = snippet.o diff.o file.o hunk.o text.o \ +DIFF_OBJS = snippet.o diff.o \ levenshtein_table.o levenshtein_distances.o QT_OBJS = diff_view.o main_window.o PARSER_OBJS = parser.o @@ -29,11 +29,13 @@ PARSER_OBJS = parser.o OBJS_TMP = main.o settings.o $(DIFF_OBJS) $(QT_OBJS) $(PARSER_OBJS) OBJS = $(foreach file, $(OBJS_TMP), objs/$(file)) + all: $(TARGETS) qshowdiff: $(OBJS) $(CC) $(ALL_FLAGS) -o $@ $(OBJS) $(ALL_LD_FLAGS) + objs/%.o: %.cpp %.h $(CC) $(ALL_FLAGS) -c -o $@ $< objs/%.o: %.cpp diff --git a/src/diff/diff.cpp b/src/diff/diff.cpp dissimilarity index 95% index 12e3fc8..486f4ea 100644 --- a/src/diff/diff.cpp +++ b/src/diff/diff.cpp @@ -1,45 +1,5 @@ -#include -#include "diff.h" - -int Diff::paintOriginal(QPainter &painter) const -{ - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - int offset = 0; - for (;it != it_end; it++){ - offset = (*it)->paintOriginal(painter, offset); - } - - return offset; -} - -int Diff::paintModified(QPainter &painter) const -{ - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - int offset = 0; - for (;it != it_end; it++){ - offset = (*it)->paintModified(painter, offset); - } - - return offset; -} - - -int Diff::paintOriginal(int pos, QPainter &painter) const -{ - return VectorOfPointers::_get(pos)->paintOriginal(painter, 0); -} - -int Diff::paintModified(int pos, QPainter &painter) const -{ - return VectorOfPointers::_get(pos)->paintModified(painter, 0); -} - -// static member -Diff Diff::diff; - +#include "diff.h" + +// static member +Diff Diff::diff; + diff --git a/src/diff/diff.h b/src/diff/diff.h index d206b3b..32f03c9 100644 --- a/src/diff/diff.h +++ b/src/diff/diff.h @@ -2,8 +2,8 @@ #define _DIFF_H_ #include -#include -#include +#include + #include "file.h" #include "../debug.h" @@ -25,13 +25,10 @@ class Diff : public VectorOfPointers{ void addFile(File *f){ VectorOfPointers::_add(f); } int numFiles() const { return VectorOfPointers::_size(); } - QString getFile(int pos) const + QString getFilename(int pos) const { return VectorOfPointers::_get(pos)->getFilename(); } - - int paintOriginal(QPainter &) const; - int paintModified(QPainter &) const; - int paintOriginal(int pos, QPainter &) const; - int paintModified(int pos, QPainter &) const; + File const *getFile(int pos) const + { return VectorOfPointers::_get(pos); } }; #endif diff --git a/src/diff/file.cpp b/src/diff/file.cpp deleted file mode 100644 index 3a0e88b..0000000 --- a/src/diff/file.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "file.h" -#include "../debug.h" -#include "../settings.h" - -int File::paintOriginal(QPainter &painter, int offset) const -{ - offset = _paintHeader(painter, offset); - - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - for (;it != it_end; it++){ - offset = (*it)->paintOriginal(painter, offset); - } - - return offset; -} -int File::paintModified(QPainter &painter, int offset) const -{ - offset = _paintHeader(painter, offset); - - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - for (;it != it_end; it++){ - offset = (*it)->paintModified(painter, offset); - } - - return offset; -} - - -// private: - -int File::_paintHeader(QPainter &painter, int offset) const -{ - QRect rect(0, offset, painter.window().width(), - Settings::File::box_height); - QRect text_rect(0 + Settings::File::text_left_indentation, offset, - painter.window().width(), Settings::File::box_height); - - // Rectangle - painter.setBrush(QBrush(Settings::File::background_color)); - painter.setPen(Settings::File::background_color); - painter.drawRect(rect); - - // Text - painter.setFont(Settings::File::font); - painter.setPen(Settings::File::font_color); - painter.drawText(text_rect, Qt::AlignVCenter, "Filename: " + _filename); - - offset += Settings::File::box_height; - - return offset; -} diff --git a/src/diff/file.h b/src/diff/file.h index d999e6a..58bd396 100644 --- a/src/diff/file.h +++ b/src/diff/file.h @@ -1,9 +1,9 @@ #ifndef _FILE_H_ #define _FILE_H_ -#include -#include #include +#include + #include "hunk.h" class File : public VectorOfPointers{ @@ -26,9 +26,6 @@ class File : public VectorOfPointers{ /** * Return filename. */ - QString getFilename() const { return _filename; } - - int paintOriginal(QPainter &original, int offset) const; - int paintModified(QPainter &original, int offset) const; + const QString &getFilename() const { return _filename; } }; #endif diff --git a/src/diff/hunk.cpp b/src/diff/hunk.cpp deleted file mode 100644 index 34ffe2e..0000000 --- a/src/diff/hunk.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "../settings.h" -#include "hunk.h" - -int Hunk::_paintSeparator(QPainter &painter, int offset) const -{ - painter.setBrush(QBrush(Settings::Hunk::separator_color)); - painter.setPen(Settings::Hunk::separator_color); - painter.drawRect(0, offset, painter.window().width(), - Settings::Hunk::separator_height); - - return offset + Settings::Hunk::separator_height; -} - - -int Hunk::paintOriginal(QPainter &painter, int offset) const -{ - int num_line = _original_from_line; - offset = _paintSeparator(painter, offset); - - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - for (;it != it_end; it++){ - offset = (*it)->paintOriginal(painter, offset, num_line); - num_line += (*it)->numOriginalLines(); - } - - return offset; -} - -int Hunk::paintModified(QPainter &painter, int offset) const -{ - int num_line = _original_from_line; - offset = _paintSeparator(painter, offset); - - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - for (;it != it_end; it++){ - offset = (*it)->paintModified(painter, offset, num_line); - num_line += (*it)->numModifiedLines(); - } - - return offset; -} diff --git a/src/diff/hunk.h b/src/diff/hunk.h index d97a30c..90e653f 100644 --- a/src/diff/hunk.h +++ b/src/diff/hunk.h @@ -2,7 +2,6 @@ #define _HUNK_H_ #include -#include #include "snippet.h" #include "utils.h" @@ -44,9 +43,5 @@ class Hunk : public VectorOfPointers{ int originalBeginsAt() const { return _original_from_line;} int modifiedBeginsAt() const { return _modified_from_line;} - - - int paintOriginal(QPainter &painter, int offset) const; - int paintModified(QPainter &painter, int offset) const; }; #endif diff --git a/src/diff/snippet.cpp b/src/diff/snippet.cpp index d1d9a6b..cf1af91 100644 --- a/src/diff/snippet.cpp +++ b/src/diff/snippet.cpp @@ -27,27 +27,5 @@ void Snippet::_free() if (_levenshtein != NULL) delete _levenshtein; } -int Snippet::_paint(Text *text, QPainter &painter, int offset, int from_line) const -{ - int lines = numLines(); - int height = QFontMetrics(Settings::Text::font).height(); - - painter.setBrush(QBrush(getBackgroundColor())); - painter.setPen(getBackgroundColor()); - painter.drawRect(0, offset, painter.window().width(), lines*height); - - if (_levenshtein == NULL){ - offset = text->paint(painter, offset, lines, from_line); - }else{ - if (text == _original) - offset = text->paint(painter, offset, lines, from_line, - _levenshtein->getRangesOriginal()); - if (text == _modified) - offset = text->paint(painter, offset, lines, from_line, - _levenshtein->getRangesModified()); - } - - return offset; -} /* vim: set sw=4 ts=4 et ft=cpp : */ diff --git a/src/diff/snippet.h b/src/diff/snippet.h index 09c5ae7..fc430c9 100644 --- a/src/diff/snippet.h +++ b/src/diff/snippet.h @@ -46,13 +46,6 @@ class Snippet{ void _copy(const Snippet &); void _free(); - /** - * Paint text by painter on offset (from beginning). from_line is - * number of line, where text begin. - */ - virtual int _paint(Text *text, QPainter &painter, int offset, - int from_line) const; - Snippet(); public: @@ -74,18 +67,14 @@ class Snippet{ /** * Returns text from original file. */ - virtual Text original() const{ return *_original;} + virtual const Text *getOriginal() const{ return _original;} /** * Returns text from modified file. */ - virtual Text modified() const{ return *_modified;} + virtual const Text *getModified() const{ return _modified;} - virtual int paintOriginal(QPainter &painter, int offset, int from_line) const - { return _paint(_original, painter, offset, from_line); } - virtual int paintModified(QPainter &painter, int offset, int from_line) const - { return _paint(_modified, painter, offset, from_line); } virtual QColor &getBackgroundColor() const { return Settings::Text::background_color; } @@ -114,7 +103,6 @@ class Added : public Snippet{ Added(Text *t, Text *tt) : Snippet(t,tt){} public: Added(Text *t) : Snippet(t){ _original = new Text();} - Text original() const{ return Text();} QColor &getBackgroundColor() const { return Settings::Text::background_color_added;} bool isAdded() const { return true; } @@ -126,7 +114,6 @@ class Deleted : public Snippet{ Deleted(Text *t, Text *tt) : Snippet(t,tt){} public: Deleted(Text *t) : Snippet(t){ _modified = new Text(); } - Text modified() const{ return Text();} QColor &getBackgroundColor() const { return Settings::Text::background_color_deleted;} bool isDeleted() const { return true; } diff --git a/src/diff/text.cpp b/src/diff/text.cpp deleted file mode 100644 index 028e6cd..0000000 --- a/src/diff/text.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "text.h" -#include "../settings.h" - -int Text::paint(QPainter &painter, int offset, int lines, int from_line) const -{ - list_of_ranges_t ranges; - return paint(painter, offset, lines, from_line, ranges); -} - -int Text::paint(QPainter &painter, int offset, int lines, int from_line, - const list_of_ranges_t &ranges) const -{ - int max_width = 0; - - QFontMetrics metrics = QFontMetrics(Settings::Text::font); - int height = metrics.height(); - - QRect line_rect(0, offset, Settings::Text::line_column_width, height); - QRect text_rect(Settings::Text::line_column_width + - Settings::Text::line_indentation, offset, - painter.window().width(), height); - int line = 0; - - painter.setBrush(Qt::NoBrush); - painter.setPen(Settings::Text::font_color); - painter.setFont(Settings::Text::font); - - VectorOfPointers::list_iterator_t it = - VectorOfPointers::_begin(); - VectorOfPointers::list_iterator_t it_end = - VectorOfPointers::_end(); - for (;it != it_end; it++, line++){ - painter.drawText(line_rect, Qt::AlignRight, - QString::number(from_line)); - line_rect.moveTo(line_rect.x(), line_rect.y() + height); - from_line++; - - max_width = std::max(max_width, metrics.width(**it)); - text_rect.setWidth(max_width); - painter.drawText(text_rect, Qt::AlignLeft, **it); - text_rect.moveTo(text_rect.x(), text_rect.y() + height); - offset += height; - } - if (line < lines) - offset += height*(lines-line); - - Settings::max_line_width = std::max(Settings::max_line_width, max_width); - - return offset; -} diff --git a/src/diff/text.h b/src/diff/text.h index 7927a28..790277d 100644 --- a/src/diff/text.h +++ b/src/diff/text.h @@ -2,7 +2,6 @@ #define _TEXT_H_ #include -#include #include #include "utils.h" @@ -15,10 +14,6 @@ class Text : public VectorOfPointers{ const QString &getLine(int num) const { return *(VectorOfPointers::_get(num)); } - - int paint(QPainter &painter, int offset, int lines, int from_line) const; - int paint(QPainter &painter, int offset, int lines, int from_line, - const list_of_ranges_t &) const; }; #endif diff --git a/src/diff/utils.h b/src/diff/utils.h index 6e5d3fd..fe8bac1 100644 --- a/src/diff/utils.h +++ b/src/diff/utils.h @@ -62,7 +62,6 @@ class VectorPointerIterator{ template class VectorOfPointers{ protected: - typedef typename std::vector::const_iterator list_iterator_t; std::vector _list;//! list of aggregated classes /** @@ -91,10 +90,11 @@ class VectorOfPointers{ */ Type *_get(int pos) const { return _list[pos]; } - list_iterator_t _begin() const { return _list.begin();} - list_iterator_t _end() const { return _list.end();} - public: + typedef typename std::vector::const_iterator const_iterator; + const_iterator begin() const { return _list.begin();} + const_iterator end() const { return _list.end();} + VectorOfPointers(){} VectorOfPointers(const VectorOfPointers &cl){ _copy(cl); } virtual ~VectorOfPointers(){ _free(); } @@ -110,8 +110,8 @@ class VectorOfPointers{ template void VectorOfPointers::_copy(const VectorOfPointers &cl) { - list_iterator_t it = cl._list.begin(); - list_iterator_t it_end = cl._list.end(); + const_iterator it = cl._list.begin(); + const_iterator it_end = cl._list.end(); for (;it != it_end; it++){ _list.push_back(new Type(**it)); } @@ -120,8 +120,8 @@ void VectorOfPointers::_copy(const VectorOfPointers &cl) template void VectorOfPointers::_free() { - list_iterator_t it = _list.begin(); - list_iterator_t it_end = _list.end(); + const_iterator it = _list.begin(); + const_iterator it_end = _list.end(); for (;it != it_end; it++){ delete *it; } @@ -136,9 +136,9 @@ bool VectorOfPointers::operator==(const VectorOfPointers &cl) const return false; // _list and t._list has same length so no t._list.end() is needed - list_iterator_t it = _list.begin(); - list_iterator_t it_end = _list.end(); - list_iterator_t it2 = cl._list.begin(); + const_iterator it = _list.begin(); + const_iterator it_end = _list.end(); + const_iterator it2 = cl._list.begin(); for (;it != it_end; it++, it2++){ if (**it != **it2) return false; diff --git a/src/qt/diff_view.cpp b/src/qt/diff_view.cpp index 241618b..bc85205 100644 --- a/src/qt/diff_view.cpp +++ b/src/qt/diff_view.cpp @@ -58,22 +58,15 @@ void DiffViewFrame::changeFile(int num) const /* DiffView */ void DiffView::paintEvent(QPaintEvent *e) { - int height; - QPainter painter(this); - if (_original){ - if (_current_file == -1) - height = Diff::instance()->paintOriginal(painter); - else - height = Diff::instance()->paintOriginal(_current_file, painter); - }else{ - if (_current_file == -1) - height = Diff::instance()->paintModified(painter); - else - height = Diff::instance()->paintModified(_current_file, painter); - } + painter = new QPainter(this); + offset = 0; + max_width = 0; - setFixedHeight(height); - setMinimumWidth(Settings::max_line_width); + _paintFiles(); + + setFixedHeight(offset); + setMinimumWidth(max_width); + delete painter; } void DiffView::setCurrentFile(int num) @@ -81,3 +74,138 @@ void DiffView::setCurrentFile(int num) _current_file = num; repaint(); } + + + + +void DiffView::_paintFiles() +{ + if (_current_file == -1){ + Diff::const_iterator it = Diff::instance()->begin(); + Diff::const_iterator it_end = Diff::instance()->end(); + + for (;it != it_end; it++){ + _paintFileHeader((*it)->getFilename()); + _paintHunks(*it); + } + }else{ + Diff *diff = Diff::instance(); + + if (diff->numFiles() > _current_file){ + File const *file = diff->getFile(_current_file); + _paintFileHeader(file->getFilename()); + _paintHunks(file); + } // else do nothing + } +} + +void DiffView::_paintHunks(File const *file) +{ + File::const_iterator it = file->begin(); + File::const_iterator it_end = file->end(); + + for (; it != it_end; it++){ + _paintHunkHeader(); + _paintSnippets(*it); + } +} + +void DiffView::_paintSnippets(Hunk const *hunk) +{ + int from_line; + int height = QFontMetrics(Settings::Text::font).height(); + Hunk::const_iterator it = hunk->begin(); + Hunk::const_iterator it_end = hunk->end(); + + if (_original) + from_line = hunk->originalBeginsAt(); + else + from_line = hunk->modifiedBeginsAt(); + + for (; it != it_end; it++){ + _paintSnippetBackground(*it); + if (_original){ + _paintText((*it)->getOriginal(), from_line); + from_line += (*it)->numOriginalLines(); + }else{ + _paintText((*it)->getModified(), from_line); + from_line += (*it)->numModifiedLines(); + } + offset += height * (*it)->numLines(); + } +} + + +void DiffView::_paintText(Text const *text, int from_line) +{ + int offset = this->offset; + + QFontMetrics metrics = QFontMetrics(Settings::Text::font); + int height = metrics.height(); + + QRect line_rect(0, offset, Settings::Text::line_column_width, height); + QRect text_rect(Settings::Text::line_column_width + + Settings::Text::line_indentation, offset, + painter->window().width(), height); + int line = 0; + + painter->setBrush(Qt::NoBrush); + painter->setPen(Settings::Text::font_color); + painter->setFont(Settings::Text::font); + + Text::const_iterator it = text->begin(); + Text::const_iterator it_end = text->end(); + for (;it != it_end; it++, line++){ + painter->drawText(line_rect, Qt::AlignRight, + QString::number(from_line)); + line_rect.moveTo(line_rect.x(), line_rect.y() + height); + from_line++; + + max_width = std::max(max_width, metrics.width(**it)); + text_rect.setWidth(max_width); + painter->drawText(text_rect, Qt::AlignLeft, **it); + text_rect.moveTo(text_rect.x(), text_rect.y() + height); + offset += height; + } +} + + +void DiffView::_paintFileHeader(const QString &filename) +{ + QRect rect(0, offset, painter->window().width(), + Settings::File::box_height); + QRect text_rect(0 + Settings::File::text_left_indentation, offset, + painter->window().width(), Settings::File::box_height); + + // Rectangle + painter->setBrush(QBrush(Settings::File::background_color)); + painter->setPen(Settings::File::background_color); + painter->drawRect(rect); + + // Text + painter->setFont(Settings::File::font); + painter->setPen(Settings::File::font_color); + painter->drawText(text_rect, Qt::AlignVCenter, "Filename: " + filename); + + offset += Settings::File::box_height; +} + +void DiffView::_paintHunkHeader() +{ + painter->setBrush(QBrush(Settings::Hunk::separator_color)); + painter->setPen(Settings::Hunk::separator_color); + painter->drawRect(0, offset, painter->window().width(), + Settings::Hunk::separator_height); + + offset += Settings::Hunk::separator_height; +} + +void DiffView::_paintSnippetBackground(Snippet const *snippet) +{ + int lines = snippet->numLines(); + int height = QFontMetrics(Settings::Text::font).height(); + + painter->setBrush(QBrush(snippet->getBackgroundColor())); + painter->setPen(snippet->getBackgroundColor()); + painter->drawRect(0, offset, painter->window().width(), lines*height); +} diff --git a/src/qt/diff_view.h b/src/qt/diff_view.h index 0fa75fd..9242cf7 100644 --- a/src/qt/diff_view.h +++ b/src/qt/diff_view.h @@ -34,6 +34,24 @@ class DiffView : public QWidget{ */ int _current_file; + + /** + * Painting: + */ + + QPainter *painter; + int offset; + int max_width; + + void _paintFiles(); + void _paintHunks(File const *); + void _paintSnippets(Hunk const *); + void _paintText(Text const *, int from_line); + + void _paintFileHeader(const QString &); + void _paintHunkHeader(); + void _paintSnippetBackground(Snippet const *); + public: DiffView(bool original, QWidget *parent = 0) : QWidget(parent), _original(original), _current_file(-1) {} diff --git a/src/qt/main_window.cpp b/src/qt/main_window.cpp index b2aca0e..11d5e3e 100644 --- a/src/qt/main_window.cpp +++ b/src/qt/main_window.cpp @@ -16,7 +16,8 @@ MainWindow::MainWindow() int len = Diff::instance()->numFiles(); DBG("Num files in Diff: " << len); for (int i=0; i < len; i++){ - top->insertItem(i+1, new QListWidgetItem(Diff::instance()->getFile(i))); + top->insertItem(i+1, + new QListWidgetItem(Diff::instance()->getFilename(i))); } top->setCurrentRow(0); -- 2.11.4.GIT