highlight (bold) search string in results

search
Marc Zinnschlag 10 years ago
parent 6d165dabb6
commit 4928e3705f

@ -79,33 +79,38 @@ void CSMTools::Search::searchRecordStateCell (const CSMWorld::IdTableBase *model
QString CSMTools::Search::formatDescription (const QString& description, int pos, int length) const
{
int padding = 10; ///< \todo make this configurable
QString text (description);
if (pos<padding)
{
length += pos;
pos = 0;
}
else
{
pos -= padding;
length += padding;
}
// compensate for Windows nonsense
text.remove ('\r');
length += padding;
// split
int padding = 10; ///< \todo make this configurable
QString text = description.mid (pos, length);
QString highlight = flatten (text.mid (pos, length));
QString before = flatten (padding<=pos ? text.mid (0, pos) : text.mid (pos-padding, padding));
QString after = flatten (text.mid (pos+length, padding));
// compensate for Windows nonsense
text.remove ('\r');
// join
text = before + "<b>" + highlight + "</b>" + after;
// improve layout for single line display
text.replace ("\n", "<CR>");
text.replace ("\n", "&lt;CR>");
text.replace ('\t', ' ');
return text;
}
QString CSMTools::Search::flatten (const QString& text) const
{
QString flat (text);
flat.replace ("&", "&amp;");
flat.replace ("<", "&lt;");
return flat;
}
CSMTools::Search::Search() : mType (Type_None) {}
CSMTools::Search::Search (Type type, const std::string& value)

@ -58,6 +58,8 @@ namespace CSMTools
QString formatDescription (const QString& description, int pos, int length) const;
QString flatten (const QString& text) const;
public:
Search();

@ -6,7 +6,7 @@
CSVTools::ReportSubView::ReportSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document)
: CSVDoc::SubView (id)
{
setWidget (mTable = new ReportTable (document, id, this));
setWidget (mTable = new ReportTable (document, id, false, this));
connect (mTable, SIGNAL (editRequest (const CSMWorld::UniversalId&, const std::string&)),
SIGNAL (focusId (const CSMWorld::UniversalId&, const std::string&)));

@ -6,11 +6,45 @@
#include <QHeaderView>
#include <QAction>
#include <QMenu>
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QPainter>
#include "../../model/tools/reportmodel.hpp"
#include "../../view/world/idtypedelegate.hpp"
namespace CSVTools
{
class RichTextDelegate : public QStyledItemDelegate
{
public:
RichTextDelegate (QObject *parent = 0);
virtual void paint(QPainter *painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
};
}
CSVTools::RichTextDelegate::RichTextDelegate (QObject *parent) : QStyledItemDelegate (parent)
{}
void CSVTools::RichTextDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QTextDocument document;
QVariant value = index.data (Qt::DisplayRole);
if (value.isValid() && !value.isNull())
{
document.setHtml (value.toString());
painter->translate (option.rect.topLeft());
document.drawContents (painter);
painter->translate (-option.rect.topLeft());
}
}
void CSVTools::ReportTable::contextMenuEvent (QContextMenuEvent *event)
{
QModelIndexList selectedRows = selectionModel()->selectedRows();
@ -67,7 +101,7 @@ void CSVTools::ReportTable::mouseDoubleClickEvent (QMouseEvent *event)
}
CSVTools::ReportTable::ReportTable (CSMDoc::Document& document,
const CSMWorld::UniversalId& id, QWidget *parent)
const CSMWorld::UniversalId& id, bool richTextDescription, QWidget *parent)
: CSVWorld::DragRecordTable (document, parent), mModel (document.getReport (id))
{
horizontalHeader()->setResizeMode (QHeaderView::Interactive);
@ -85,6 +119,9 @@ CSVTools::ReportTable::ReportTable (CSMDoc::Document& document,
setItemDelegateForColumn (0, mIdTypeDelegate);
if (richTextDescription)
setItemDelegateForColumn (mModel->columnCount()-1, new RichTextDelegate (this));
mShowAction = new QAction (tr ("Show"), this);
connect (mShowAction, SIGNAL (triggered()), this, SLOT (showSelection()));
addAction (mShowAction);

@ -36,8 +36,9 @@ namespace CSVTools
public:
/// \param richTextDescription Use rich text in the description column.
ReportTable (CSMDoc::Document& document, const CSMWorld::UniversalId& id,
QWidget *parent = 0);
bool richTextDescription, QWidget *parent = 0);
virtual std::vector<CSMWorld::UniversalId> getDraggedRecords() const;

@ -17,7 +17,7 @@ CSVTools::SearchSubView::SearchSubView (const CSMWorld::UniversalId& id, CSMDoc:
layout->addWidget (&mSearchBox);
layout->addWidget (mTable = new ReportTable (document, id), 2);
layout->addWidget (mTable = new ReportTable (document, id, true), 2);
QWidget *widget = new QWidget;

Loading…
Cancel
Save