got rid of the preview tabdata array

This commit is contained in:
dockes 2009-09-29 08:47:25 +00:00
parent 002eebd5a1
commit d13431f46e
2 changed files with 77 additions and 107 deletions

View File

@ -155,7 +155,6 @@ void Preview::init()
pvEdit->setUndoRedoEnabled(FALSE); pvEdit->setUndoRedoEnabled(FALSE);
unnamedLayout->addWidget(pvEdit); unnamedLayout->addWidget(pvEdit);
pvTab->insertTab(unnamed, QString::fromLatin1("")); pvTab->insertTab(unnamed, QString::fromLatin1(""));
m_tabData.push_back(TabData(pvTab->currentPage()));
previewLayout->addWidget(pvTab); previewLayout->addWidget(pvTab);
@ -249,6 +248,8 @@ bool Preview::eventFilter(QObject *target, QEvent *event)
return false; return false;
LOGDEB1(("Preview::eventFilter: keyEvent\n")); LOGDEB1(("Preview::eventFilter: keyEvent\n"));
PreviewTextEdit *edit = currentEditor();
QKeyEvent *keyEvent = (QKeyEvent *)event; QKeyEvent *keyEvent = (QKeyEvent *)event;
if (keyEvent->key() == Qt::Key_Q && if (keyEvent->key() == Qt::Key_Q &&
(keyEvent->state() & Qt::ControlButton)) { (keyEvent->state() & Qt::ControlButton)) {
@ -260,16 +261,14 @@ bool Preview::eventFilter(QObject *target, QEvent *event)
} else if (keyEvent->key() == Qt::Key_Down && } else if (keyEvent->key() == Qt::Key_Down &&
(keyEvent->state() & Qt::ShiftButton)) { (keyEvent->state() & Qt::ShiftButton)) {
// LOGDEB(("Preview::eventFilter: got Shift-Up\n")); // LOGDEB(("Preview::eventFilter: got Shift-Up\n"));
TabData *d = tabDataForCurrent(); if (edit)
if (d) emit(showNext(this, m_searchId, edit->m_data.docnum));
emit(showNext(this, m_searchId, d->docnum));
return true; return true;
} else if (keyEvent->key() == Qt::Key_Up && } else if (keyEvent->key() == Qt::Key_Up &&
(keyEvent->state() & Qt::ShiftButton)) { (keyEvent->state() & Qt::ShiftButton)) {
// LOGDEB(("Preview::eventFilter: got Shift-Down\n")); // LOGDEB(("Preview::eventFilter: got Shift-Down\n"));
TabData *d = tabDataForCurrent(); if (edit)
if (d) emit(showPrev(this, m_searchId, edit->m_data.docnum));
emit(showPrev(this, m_searchId, d->docnum));
return true; return true;
} else if (keyEvent->key() == Qt::Key_W && } else if (keyEvent->key() == Qt::Key_W &&
(keyEvent->state() & Qt::ControlButton)) { (keyEvent->state() & Qt::ControlButton)) {
@ -289,21 +288,16 @@ bool Preview::eventFilter(QObject *target, QEvent *event)
if (target != searchTextLine) if (target != searchTextLine)
return QApplication::sendEvent(searchTextLine, event); return QApplication::sendEvent(searchTextLine, event);
} else { } else {
QWidget *tw = pvTab->currentPage(); if (edit && target == edit) {
PreviewTextEdit *e = 0;
if (tw)
e = (PreviewTextEdit *)tw->child("pvEdit");
LOGDEB1(("Widget: %p, edit %p, target %p\n", tw, e, target));
if (e && target == e) {
if (keyEvent->key() == Qt::Key_Slash) { if (keyEvent->key() == Qt::Key_Slash) {
searchTextLine->setFocus(); searchTextLine->setFocus();
m_dynSearchActive = true; m_dynSearchActive = true;
return true; return true;
} else if (keyEvent->key() == Qt::Key_Space) { } else if (keyEvent->key() == Qt::Key_Space) {
e->scrollBy(0, e->visibleHeight()); edit->scrollBy(0, edit->visibleHeight());
return true; return true;
} else if (keyEvent->key() == Qt::Key_BackSpace) { } else if (keyEvent->key() == Qt::Key_BackSpace) {
e->scrollBy(0, -e->visibleHeight()); edit->scrollBy(0, -edit->visibleHeight());
return true; return true;
} }
} }
@ -334,12 +328,12 @@ void Preview::searchTextLine_textChanged(const QString & text)
#define QStyleSheetItem Q3StyleSheetItem #define QStyleSheetItem Q3StyleSheetItem
#endif #endif
PreviewTextEdit *Preview::getCurrentEditor() PreviewTextEdit *Preview::currentEditor()
{ {
QWidget *tw = pvTab->currentPage(); QWidget *tw = pvTab->currentPage();
PreviewTextEdit *edit = 0; PreviewTextEdit *edit = 0;
if (tw) { if (tw) {
edit = (PreviewTextEdit*)tw->child("pvEdit"); edit = dynamic_cast<PreviewTextEdit*>(tw->child("pvEdit"));
} }
return edit; return edit;
} }
@ -357,7 +351,7 @@ void Preview::doSearch(const QString &_text, bool next, bool reverse,
QString text = _text; QString text = _text;
bool matchCase = matchCheck->isChecked(); bool matchCase = matchCheck->isChecked();
PreviewTextEdit *edit = getCurrentEditor(); PreviewTextEdit *edit = currentEditor();
if (edit == 0) { if (edit == 0) {
// ?? // ??
return; return;
@ -440,7 +434,8 @@ void Preview::prevPressed()
// Called when user clicks on tab // Called when user clicks on tab
void Preview::currentChanged(QWidget * tw) void Preview::currentChanged(QWidget * tw)
{ {
QWidget *edit = (QWidget *)tw->child("pvEdit"); PreviewTextEdit *edit =
dynamic_cast<PreviewTextEdit*>(tw->child("pvEdit"));
m_currentW = tw; m_currentW = tw;
LOGDEB1(("Preview::currentChanged(). Editor: %p\n", edit)); LOGDEB1(("Preview::currentChanged(). Editor: %p\n", edit));
@ -463,9 +458,7 @@ void Preview::currentChanged(QWidget * tw)
#endif #endif
tw->installEventFilter(this); tw->installEventFilter(this);
edit->installEventFilter(this); edit->installEventFilter(this);
TabData *d = tabDataForCurrent(); emit(previewExposed(this, m_searchId, edit->m_data.docnum));
if (d)
emit(previewExposed(this, m_searchId, d->docnum));
} }
#if (QT_VERSION >= 0x040000) #if (QT_VERSION >= 0x040000)
@ -527,14 +520,6 @@ void Preview::closeCurrentTab()
if (!tw) if (!tw)
return; return;
pvTab->removePage(tw); pvTab->removePage(tw);
// Have to remove from tab data list
for (list<TabData>::iterator it = m_tabData.begin();
it != m_tabData.end(); it++) {
if (it->w == tw) {
m_tabData.erase(it);
return;
}
}
} else { } else {
close(); close();
} }
@ -550,7 +535,6 @@ PreviewTextEdit *Preview::addEditorTab()
anonLayout->addWidget(editor); anonLayout->addWidget(editor);
pvTab->addTab(anon, "Tab"); pvTab->addTab(anon, "Tab");
pvTab->showPage(anon); pvTab->showPage(anon);
m_tabData.push_back(TabData(anon));
return editor; return editor;
} }
@ -586,41 +570,33 @@ void Preview::setCurTabProps(const string &fn, const Rcl::Doc &doc,
tiptxt += meta_it->second + "\n"; tiptxt += meta_it->second + "\n";
pvTab->setTabToolTip(w,QString::fromUtf8(tiptxt.c_str(), tiptxt.length())); pvTab->setTabToolTip(w,QString::fromUtf8(tiptxt.c_str(), tiptxt.length()));
TabData *d = tabDataForCurrent(); PreviewTextEdit *e = currentEditor();
if (d) { if (e) {
d->fn = fn; e->m_data.fn = fn;
d->ipath = doc.ipath; e->m_data.ipath = doc.ipath;
d->docnum = docnum; e->m_data.docnum = docnum;
} }
} }
TabData *Preview::tabDataForCurrent()
{
QWidget *w = pvTab->currentPage();
if (w == 0)
return 0;
for (list<TabData>::iterator it = m_tabData.begin();
it != m_tabData.end(); it++) {
if (it->w == w) {
return &(*it);
}
}
return 0;
}
bool Preview::makeDocCurrent(const string &fn, size_t sz, bool Preview::makeDocCurrent(const string &fn, size_t sz,
const Rcl::Doc& doc, int docnum, bool sametab) const Rcl::Doc& doc, int docnum, bool sametab)
{ {
LOGDEB(("Preview::makeDocCurrent: %s\n", fn.c_str())); LOGDEB(("Preview::makeDocCurrent: %s\n", fn.c_str()));
for (list<TabData>::iterator it = m_tabData.begin();
it != m_tabData.end(); it++) { /* Check if we already have this page */
LOGDEB2(("Preview::makeFileCurrent: compare to w %p, file %s\n", for (int i = 0; i < pvTab->count(); i++) {
it->w, it->fn.c_str())); QWidget *tw = pvTab->widget(i);
if (!it->fn.compare(fn) && !it->ipath.compare(doc.ipath)) { if (tw) {
pvTab->showPage(it->w); PreviewTextEdit *edit =
return true; dynamic_cast<PreviewTextEdit*>(tw->child("pvEdit"));
} if (edit && !edit->m_data.fn.compare(fn) &&
!edit->m_data.ipath.compare(doc.ipath)) {
pvTab->showPage(tw);
return true;
}
}
} }
// if just created the first tab was created during init // if just created the first tab was created during init
if (!sametab && !m_justCreated && !addEditorTab()) { if (!sametab && !m_justCreated && !addEditorTab()) {
return false; return false;
@ -923,7 +899,7 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc,
} }
// Load into editor // Load into editor
PreviewTextEdit *editor = getCurrentEditor(); PreviewTextEdit *editor = currentEditor();
editor->setText(""); editor->setText("");
if (highlightTerms) { if (highlightTerms) {
QStyleSheetItem *item = QStyleSheetItem *item =
@ -931,7 +907,6 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc,
item->setColor(prefs.qtermcolor); item->setColor(prefs.qtermcolor);
item->setFontWeight(QFont::Bold); item->setFontWeight(QFont::Bold);
} }
TabData *d = tabDataForCurrent();
prog = 2 * nsteps / 3; prog = 2 * nsteps / 3;
progress.setLabelText(tr("Loading preview text into editor")); progress.setLabelText(tr("Loading preview text into editor"));
@ -944,9 +919,8 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc,
editor->append(*it); editor->append(*it);
// We need to save the rich text for printing, the editor does // We need to save the rich text for printing, the editor does
// not do it for us // not do it consistently for us.
if (d) editor->m_data.richtxt.append(*it);
d->richtxt.append(*it);
// Stay at top // Stay at top
if (instep < 5) { if (instep < 5) {
@ -962,10 +936,10 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc,
} }
progress.close(); progress.close();
if (d) {
fdoc.text.clear(); fdoc.text.clear();
d->fdoc = fdoc; editor->m_data.fdoc = fdoc;
}
m_haveAnchors = m_plaintorich.lastanchor != 0; m_haveAnchors = m_plaintorich.lastanchor != 0;
if (searchTextLine->text().length() != 0) { if (searchTextLine->text().length() != 0) {
// If there is a current search string, perform the search // If there is a current search string, perform the search
@ -994,7 +968,7 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc,
RCLPOPUP *PreviewTextEdit::createPopupMenu(const QPoint&) RCLPOPUP *PreviewTextEdit::createPopupMenu(const QPoint&)
{ {
RCLPOPUP *popup = new RCLPOPUP(this); RCLPOPUP *popup = new RCLPOPUP(this);
if (m_savedText.isEmpty()) { if (!m_dspflds) {
popup->insertItem(tr("Show fields"), this, SLOT(toggleFields())); popup->insertItem(tr("Show fields"), this, SLOT(toggleFields()));
} else { } else {
popup->insertItem(tr("Show main text"), this, SLOT(toggleFields())); popup->insertItem(tr("Show main text"), this, SLOT(toggleFields()));
@ -1009,29 +983,26 @@ void PreviewTextEdit::toggleFields()
// fprintf(stderr, "%s", (const char *)text().ascii()); // fprintf(stderr, "%s", (const char *)text().ascii());
// If currently displaying fields, switch to body text // If currently displaying fields, switch to body text
if (!m_savedText.isEmpty()) { if (m_dspflds) {
setText(m_savedText); setText(m_data.richtxt);
m_savedText = ""; m_dspflds = false;
return; return;
} }
// Else display fields // Else display fields
TabData *d = m_preview->tabDataForCurrent(); m_dspflds = true;
if (!d)
return;
QString txt = "<html><head></head><body>\n"; QString txt = "<html><head></head><body>\n";
txt += "<b>" + QString::fromLocal8Bit(d->fn.c_str()); txt += "<b>" + QString::fromLocal8Bit(m_data.fn.c_str());
if (!d->ipath.empty()) if (!m_data.ipath.empty())
txt += "|" + QString::fromUtf8(d->ipath.c_str()); txt += "|" + QString::fromUtf8(m_data.ipath.c_str());
txt += "</b><br><br>"; txt += "</b><br><br>";
txt += "<dl>\n"; txt += "<dl>\n";
for (map<string,string>::const_iterator it = d->fdoc.meta.begin(); for (map<string,string>::const_iterator it = m_data.fdoc.meta.begin();
it != d->fdoc.meta.end(); it++) { it != m_data.fdoc.meta.end(); it++) {
txt += "<dt>" + QString::fromUtf8(it->first.c_str()) + "</dt> " txt += "<dt>" + QString::fromUtf8(it->first.c_str()) + "</dt> "
+ "<dd>" + QString::fromUtf8(it->second.c_str()) + "</dd>\n"; + "<dd>" + QString::fromUtf8(it->second.c_str()) + "</dd>\n";
} }
txt += "</dl></body></html>"; txt += "</dl></body></html>";
m_savedText = text();
setText(txt); setText(txt);
} }
@ -1039,9 +1010,6 @@ void PreviewTextEdit::print()
{ {
if (!m_preview) if (!m_preview)
return; return;
TabData *d = m_preview->tabDataForCurrent();
if (d == 0)
return;
#ifndef QT_NO_PRINTER #ifndef QT_NO_PRINTER
QPrinter printer; QPrinter printer;
@ -1070,8 +1038,9 @@ void PreviewTextEdit::print()
metrics.height() - margin * dpiy / 72 * 2 ); metrics.height() - margin * dpiy / 72 * 2 );
QFont font( "times", 10 ); QFont font( "times", 10 );
// Dont want to use text() here, this is the plain text. We // Dont want to use text() here, this is the plain text. We
// want the rich text. // want the rich text. For some reason we don't need this for fields??
QSimpleRichText richText(d->richtxt, font, this->context(), const QString &richtxt = m_dspflds ? text() : m_data.richtxt;
QSimpleRichText richText(richtxt, font, this->context(),
this->styleSheet(), this->styleSheet(),
this->mimeSourceFactory(), body.height() ); this->mimeSourceFactory(), body.height() );
richText.setWidth( &p, body.width() ); richText.setWidth( &p, body.width() );

View File

@ -48,39 +48,43 @@ class Q3PopupMenu;
#define QTEXTEDIT Q3TextEdit #define QTEXTEDIT Q3TextEdit
#endif #endif
// We keep a list of data associated to each tab
class TabData {
public:
string fn; // filename for this tab
string ipath; // Internal doc path inside file
int docnum; // Index of doc in db search results.
// doc out of internfile (previous fields come from the index) with
// main text erased (for space).
Rcl::Doc fdoc;
// Saved rich text: the textedit seems to sometimes (but not
// always) return its text stripped of tags, so this is needed
// (for printing for example)
QString richtxt;
TabData()
: docnum(-1)
{}
};
class PreviewTextEdit : public QTEXTEDIT { class PreviewTextEdit : public QTEXTEDIT {
Q_OBJECT Q_OBJECT
public: public:
PreviewTextEdit(QWidget* parent, const char* name, Preview *pv) PreviewTextEdit(QWidget* parent, const char* name, Preview *pv)
: QTEXTEDIT(parent, name), m_preview(pv) : QTEXTEDIT(parent, name), m_preview(pv), m_dspflds(false)
{} {}
void moveToAnchor(const QString& name); void moveToAnchor(const QString& name);
public slots: public slots:
virtual void toggleFields(); virtual void toggleFields();
virtual void print(); virtual void print();
friend class Preview;
private: private:
virtual RCLPOPUP *createPopupMenu(const QPoint& pos); virtual RCLPOPUP *createPopupMenu(const QPoint& pos);
Preview *m_preview; Preview *m_preview;
QString m_savedText; TabData m_data;
bool m_dspflds;
}; };
// We keep a list of data associated to each tab
class TabData {
public:
string fn; // filename for this tab
string ipath; // Internal doc path inside file
QWidget *w; // widget for setCurrent
int docnum; // Index of doc in db search results.
// doc out of internfile (previous fields come from the index) with
// main text erased (for space).
Rcl::Doc fdoc;
QString richtxt;
TabData(QWidget *wi)
: w(wi), docnum(-1)
{}
};
// Subclass plainToRich to add <termtag>s and anchors to the preview text // Subclass plainToRich to add <termtag>s and anchors to the preview text
class PlainToRichQtPreview : public PlainToRich { class PlainToRichQtPreview : public PlainToRich {
public: public:
@ -161,7 +165,6 @@ private:
bool m_dynSearchActive; bool m_dynSearchActive;
bool m_canBeep; bool m_canBeep;
bool m_loading; bool m_loading;
list<TabData> m_tabData;
QWidget *m_currentW; QWidget *m_currentW;
HiliteData m_hData; HiliteData m_hData;
bool m_justCreated; // First tab create is different bool m_justCreated; // First tab create is different
@ -181,12 +184,10 @@ private:
void init(); void init();
virtual void setCurTabProps(const string& fn, const Rcl::Doc& doc, virtual void setCurTabProps(const string& fn, const Rcl::Doc& doc,
int docnum); int docnum);
virtual PreviewTextEdit *getCurrentEditor(); virtual PreviewTextEdit *currentEditor();
virtual PreviewTextEdit *addEditorTab(); virtual PreviewTextEdit *addEditorTab();
virtual bool loadFileInCurrentTab(string fn, size_t sz, virtual bool loadFileInCurrentTab(string fn, size_t sz,
const Rcl::Doc& idoc, int dnm); const Rcl::Doc& idoc, int dnm);
// Return auxiliary data pointer for cur tab
TabData *tabDataForCurrent();
}; };
#endif /* _PREVIEW_W_H_INCLUDED_ */ #endif /* _PREVIEW_W_H_INCLUDED_ */