Commit 1119c7de authored by Kåre Särs's avatar Kåre Särs

Add a delegate to enable formating of the search results

Thanks KDevelop for the ideas :)
parent d3c261c5
......@@ -14,6 +14,7 @@ set(katesearchplugin_PART_SRCS
plugin_search.cpp
search_open_files.cpp
search_folder.cpp
htmldelegate.cpp
)
kde4_add_ui_files(katesearchplugin_PART_SRCS search.ui)
......
/***************************************************************************
* This file is part of Kate search plugin *
* Copyright 2011 Kåre Särs <kare.sars@iki.fi> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library 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 Library 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 "htmldelegate.h"
#include <QtGui/QPainter>
#include <QtCore/QModelIndex>
#include <QtGui/QTextDocument>
#include <QtGui/QAbstractTextDocumentLayout>
#include <QtGui/QTextCharFormat>
#include <KLocalizedString>
SPHtmlDelegate::SPHtmlDelegate( QObject* parent )
: QStyledItemDelegate(parent)
{}
SPHtmlDelegate::~SPHtmlDelegate() {}
void SPHtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);
QTextDocument doc;
doc.setDocumentMargin(0);
doc.setHtml(index.data().toString());
painter->save();
options.text = QString(); // clear old text
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
// draw area
QRect clip = options.widget->style()->subElementRect(QStyle::SE_ItemViewItemText, &options);
QFontMetrics metrics(options.font);
painter->translate(clip.topLeft() - QPoint(0, metrics.descent()));
QAbstractTextDocumentLayout::PaintContext pcontext;
doc.documentLayout()->draw(painter, pcontext);
painter->restore();
}
QSize SPHtmlDelegate::sizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& index) const
{
QTextDocument doc;
doc.setDocumentMargin(0);
doc.setHtml(index.data().toString());
return doc.size().toSize();;
}
/***************************************************************************
* This file is part of Kate search plugin *
* Copyright 2011 Kåre Särs <kare.sars@iki.fi> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library 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 Library 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. *
***************************************************************************/
#ifndef HTML_DELEGATE_H
#define HTML_DELEGATE_H
#include <QtGui/QStyledItemDelegate>
class SPHtmlDelegate : public QStyledItemDelegate
{
public:
explicit SPHtmlDelegate(QObject* parent);
virtual ~SPHtmlDelegate();
void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
};
#endif
......@@ -21,6 +21,8 @@
#include "plugin_search.h"
#include "plugin_search.moc"
#include "htmldelegate.h"
#include <kate/application.h>
#include <ktexteditor/editor.h>
......@@ -333,17 +335,23 @@ QTreeWidgetItem * KatePluginSearchView::rootFileItem(const QString &url)
return 0;
}
KUrl kurl(url);
QString path = kurl.isLocalFile() ? kurl.path() : kurl.upUrl().url();
QString name = kurl.fileName();
for (int i=0; i<m_curResultTree->topLevelItemCount(); i++) {
if (m_curResultTree->topLevelItem(i)->data(0, Qt::UserRole).toString() == url) {
int matches = m_curResultTree->topLevelItem(i)->data(1, Qt::UserRole).toInt() + 1;
QString tmpUrl = QString("%1: %2").arg(url).arg(matches);
QString tmpUrl = QString("%1<b>%2</b>: <b>%3</b>").arg(path).arg(name).arg(matches);
m_curResultTree->topLevelItem(i)->setData(0, Qt::DisplayRole, tmpUrl);
m_curResultTree->topLevelItem(i)->setData(1, Qt::UserRole, matches);
return m_curResultTree->topLevelItem(i);
}
}
// file item not found create a new one
QTreeWidgetItem *item = new QTreeWidgetItem(m_curResultTree, QStringList(url + ": 1"));
QString tmpUrl = QString("%1<b>%2</b>: <b>%3</b>").arg(path).arg(name).arg(1);
QTreeWidgetItem *item = new QTreeWidgetItem(m_curResultTree, QStringList(tmpUrl));
item->setData(0, Qt::UserRole, url);
item->setData(1, Qt::UserRole, 1);
return item;
......@@ -355,8 +363,11 @@ void KatePluginSearchView::matchFound(const QString &url, int line, int column,
if (!m_curResultTree) {
return;
}
QString bold = lineContent.left(column);
bold += "<b>" + lineContent.mid(column, matchLen) + "</b>";
bold += lineContent.mid(column + matchLen);
QStringList row;
row << i18n("Line: %1 Column: %2", line+1, column+1) << lineContent.simplified();
row << i18n("Line: <b>%1</b>: %2", line+1, bold);
QTreeWidgetItem *item = new QTreeWidgetItem(rootFileItem(url), row);
item->setData(0, Qt::UserRole, url);
......@@ -400,7 +411,6 @@ void KatePluginSearchView::clearMarks()
while (i.hasNext()) {
i.next();
if (i.value()->type == KTextEditor::MarkInterface::markType32) {
kDebug();
iface->removeMark(i.value()->line, i.value()->type);
}
}
......@@ -424,10 +434,7 @@ void KatePluginSearchView::searchDone()
return;
}
m_curResultTree->resizeColumnToContents(0);
m_curResultTree->resizeColumnToContents(1);
if (m_curResultTree->columnWidth(0) > (m_curResultTree->width()/3)) {
m_curResultTree->setColumnWidth(0, m_curResultTree->width()/3);
}
if (m_curResultTree->topLevelItemCount() > 0) {
m_curResultTree->setCurrentItem(m_curResultTree->topLevelItem(0));
m_curResultTree->setFocus(Qt::OtherFocusReason);
......@@ -506,9 +513,8 @@ void KatePluginSearchView::writeSessionConfig(KConfigBase* config, const QString
void KatePluginSearchView::addTab()
{
QTreeWidget *tmp = new QTreeWidget();
QStringList labels;
labels << i18n("File") << i18n("Text");
tmp->setHeaderLabels(labels);
tmp->header()->setStretchLastSection(false);
tmp->setHeaderHidden(true);
tmp->setTextElideMode(Qt::ElideLeft);
tmp->setAllColumnsShowFocus(false);
tmp->setAlternatingRowColors(true);
......@@ -516,7 +522,8 @@ void KatePluginSearchView::addTab()
tmp->setRootIsDecorated(true);
tmp->setSelectionBehavior(QAbstractItemView::SelectRows);
tmp->setUniformRowHeights(true);
tmp->setItemDelegate(new SPHtmlDelegate(tmp));
connect(tmp, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
this, SLOT (itemSelected(QTreeWidgetItem*)));
......
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