Switch to unified view

a b/src/qtgui/snippets_w.cpp
1
/* Copyright (C) 2012 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#include "autoconfig.h"
18
19
#include <unistd.h>
20
#include <stdio.h>
21
22
#include <string>
23
#include <vector>
24
using namespace std;
25
26
#include "debuglog.h"
27
#include "recoll.h"
28
#include "snippets_w.h"
29
#include "guiutils.h"
30
#include "rcldb.h"
31
#include "rclhelp.h"
32
#include "plaintorich.h"
33
34
class PlainToRichQtSnippets : public PlainToRich {
35
public:
36
    virtual string startMatch(unsigned int)
37
    {
38
  return string("<span class='rclmatch' style='color: ")
39
      + string((const char *)prefs.qtermcolor.toAscii()) + string("'>");
40
    }
41
    virtual string endMatch() 
42
    {
43
  return string("</span>");
44
    }
45
};
46
static PlainToRichQtSnippets g_hiliter;
47
48
void SnippetsW::init()
49
{
50
    if (m_source.isNull())
51
  return;
52
53
    vector<pair<int, string> > vpabs;
54
    m_source->getAbstract(m_doc, vpabs);
55
56
    HighlightData hdata;
57
    m_source->getTerms(hdata);
58
59
    QString html = QString::fromAscii(
60
  "<html><head>"
61
  "<meta http-equiv=\"content-type\" "
62
  "content=\"text/html; charset=utf-8\"></head>"
63
  "<body style='overflow-x: scroll; white-space: nowrap'>");
64
65
    g_hiliter.set_inputhtml(false);
66
67
    for (vector<pair<int, string> >::const_iterator it = vpabs.begin(); 
68
   it != vpabs.end(); it++) {
69
  if (it->first > 0) {
70
      char buf[100];
71
      sprintf(buf, "P. %d", it->first);
72
      html += "<a href=\"";
73
      html += buf;
74
      html += "\">";
75
      html += buf;
76
      html += "</a> ";
77
  }
78
  list<string> lr;
79
  g_hiliter.plaintorich(it->second, lr, hdata);
80
  html.append(QString::fromUtf8(lr.front().c_str()));
81
  html.append("<br>\n");
82
    }
83
    html.append("</body></html>");
84
    webView->setHtml(html);
85
    connect(webView, SIGNAL(linkClicked(const QUrl &)), 
86
      this, SLOT(linkWasClicked(const QUrl &)));
87
    webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
88
}
89
90
91
void SnippetsW::linkWasClicked(const QUrl &url)
92
{
93
    string ascurl = (const char *)url.toString().toAscii();;
94
    LOGDEB(("Snippets::linkWasClicked: [%s]\n", ascurl.c_str()));
95
96
    if (ascurl.size() > 3) {
97
  int what = ascurl[0];
98
  switch (what) {
99
  case 'P': 
100
  {
101
      int page = atoi(ascurl.c_str()+2);
102
      emit startNativeViewer(m_doc, page);
103
      return;
104
  }
105
  }
106
    }
107
    LOGERR(("Snippets::linkWasClicked: bad link [%s]\n", ascurl.c_str()));
108
}
109