22#include <tqclipboard.h>
25#include <tdepopupmenu.h>
27#include <kiconloader.h>
29#include <tdeglobalsettings.h>
30#include <tdeapplication.h>
32#include "kexiquerydesignersqlhistory.h"
34KexiQueryDesignerSQLHistory::KexiQueryDesignerSQLHistory(TQWidget *parent,
const char *name)
35 : TQScrollView(parent, name)
37 viewport()->setPaletteBackgroundColor(white);
40 m_history =
new History();
41 m_history->setAutoDelete(
true);
43 m_popup =
new TDEPopupMenu(
this);
44 m_popup->insertItem(SmallIcon(
"edit-copy"), i18n(
"Copy to Clipboard"),
this, TQ_SLOT(slotToClipboard()));
47KexiQueryDesignerSQLHistory::~KexiQueryDesignerSQLHistory()
52KexiQueryDesignerSQLHistory::drawContents(TQPainter *p,
int cx,
int cy,
int cw,
int ch)
54 TQRect clipping(cx, cy, cw, ch);
57 for(HistoryEntry *it = m_history->first(); it; it = m_history->next())
60 if(clipping.intersects(it->geometry(y, visibleWidth(), fontMetrics())))
64 it->drawItem(p, visibleWidth(), colorGroup());
65 p->restoreWorldMatrix();
67 y += it->geometry(y, visibleWidth(), fontMetrics()).height() + 5;
72KexiQueryDesignerSQLHistory::contentsMousePressEvent(TQMouseEvent * e)
75 HistoryEntry *popupHistory = 0;
77 for(TQPtrListIterator<HistoryEntry> it(*m_history); it.current(); ++it)
79 if(it.current()->isSelected())
82 it.current()->setSelected(
false, colorGroup());
83 updateContents(it.current()->geometry(y, visibleWidth(), fontMetrics()));
86 if(it.current()->geometry(y, visibleWidth(), fontMetrics()).contains(e->pos()))
88 popupHistory = it.current();
91 y += it.current()->geometry(y, visibleWidth(), fontMetrics()).height() + 5;
96 if (m_selected && m_selected != popupHistory) {
97 m_selected->setSelected(
false, colorGroup());
98 updateContents(m_selected->geometry(pos, visibleWidth(), fontMetrics()));
100 m_selected = popupHistory;
101 m_selected->setSelected(
true, colorGroup());
102 updateContents(m_selected->geometry(pos, visibleWidth(), fontMetrics()));
103 if(e->button() == TQt::RightButton) {
104 m_popup->exec(e->globalPos());
110KexiQueryDesignerSQLHistory::contentsMouseDoubleClickEvent(TQMouseEvent * e)
112 contentsMousePressEvent(e);
114 emit currentItemDoubleClicked();
118KexiQueryDesignerSQLHistory::addEvent(
const TQString& q,
bool s,
const TQString &error)
120 HistoryEntry *he=m_history->last();
122 if (he->statement()==q) {
123 he->updateTime(TQTime::currentTime());
128 addEntry(
new HistoryEntry(s, TQTime::currentTime(), q, error));
132KexiQueryDesignerSQLHistory::addEntry(HistoryEntry *e)
134 m_history->append(e);
138 for(HistoryEntry *it = m_history->first(); it; it = m_history->next())
140 y += it->geometry(y, visibleWidth(), fontMetrics()).height() + 5;
143 resizeContents(visibleWidth() - 1, y);
145 m_selected->setSelected(
false, colorGroup());
148 m_selected->setSelected(
true, colorGroup());
149 ensureVisible(0,y+5);
178KexiQueryDesignerSQLHistory::slotToClipboard()
183 TQApplication::clipboard()->setText(m_selected->statement(), TQClipboard::Clipboard);
187KexiQueryDesignerSQLHistory::slotEdit()
189 emit editRequested(m_selected->statement());
193KexiQueryDesignerSQLHistory::selectedStatement()
const
195 return m_selected ? m_selected->statement() : TQString();
199KexiQueryDesignerSQLHistory::setHistory(History *h)
205void KexiQueryDesignerSQLHistory::clear()
212TDEPopupMenu* KexiQueryDesignerSQLHistory::popupMenu()
const
219HistoryEntry::HistoryEntry(
bool succeed,
const TQTime &execTime,
const TQString &statement,
const TQString &err)
222 m_execTime = execTime;
223 m_statement = statement;
226 highlight(TQColorGroup());
230HistoryEntry::drawItem(TQPainter *p,
int width,
const TQColorGroup &cg)
232 p->setPen(TQColor(200, 200, 200));
233 p->setBrush(TQColor(200, 200, 200));
234 p->drawRect(2, 2, 200, 20);
235 p->setPen(TQColor(0, 0, 0));
238 p->drawPixmap(4, 4, SmallIcon(
"button_ok"));
240 p->drawPixmap(4, 4, SmallIcon(
"button_cancel"));
242 p->drawText(22, 2, 180, 20, TQt::AlignLeft | TQt::AlignVCenter, m_execTime.toString());
243 p->setPen(TQColor(200, 200, 200));
244 p->setBrush(TQColor(255, 255, 255));
245 m_formated->setWidth(width - 2);
246 TQRect content(2, 21, width - 2, m_formated->height());
252 p->setBrush(cg.highlight());
254 p->drawRect(content);
257 p->setPen(cg.text());
259 p->setPen(cg.highlightedText());
261 content.setX(content.x() + 2);
262 content.setWidth(content.width() - 2);
264 m_formated->draw(p, content.x(), content.y(), content, cg);
268HistoryEntry::highlight(
const TQColorGroup &cg)
273 bool dblquote =
false;
275 statement = m_statement;
276 statement.replace(
"<",
"<");
277 statement.replace(
">",
">");
278 statement.replace(
"\r\n",
"<br>");
279 statement.replace(
"\n",
"<br>");
280 statement.replace(
" ",
" ");
281 statement.replace(
"\t",
" ");
286 for(
int i=0; i < (int)statement.length(); i++)
290 TQChar curr = TQChar(statement[i]);
292 if(TQString(curr) ==
"'" && !dblquote && TQString(TQChar(statement[i-1])) !=
"\\")
297 beginTag +=
"<font color=\"#ff0000\">";
305 if(TQString(curr) ==
"\"" && !quote && TQString(TQChar(statement[i-1])) !=
"\\")
310 beginTag +=
"<font color=\"#ff0000\">";
318 if(TQRegExp(
"[0-9]").exactMatch(TQString(curr)) && !quote && !dblquote)
320 beginTag +=
"<font color=\"#0000ff\">";
324 text += beginTag + curr + endTag;
329 text = TQString(
"<font color=\"%1\">%2").arg(cg.highlightedText().name()).arg(statement);
332 TQRegExp keywords(
"\\b(SELECT|UPDATE|INSERT|DELETE|DROP|FROM|WHERE|AND|OR|NOT|NULL|JOIN|LEFT|RIGHT|ON|INTO|TABLE)\\b");
333 keywords.setCaseSensitive(
false);
334 text = text.replace(keywords,
"<b>\\1</b>");
336 if(!m_error.isEmpty())
339 text += TQString(
"<br><font face=\"") + TDEGlobalSettings::generalFont().family() + TQString(
"\">") + i18n(
"Error: %1").arg(m_error) +
"</font>";
341 kdDebug() <<
"HistoryEntry::highlight() text:" << text << endl;
343 m_formated =
new TQSimpleRichText(text, TDEGlobalSettings::fixedFont());
348HistoryEntry::setSelected(
bool selected,
const TQColorGroup &cg)
350 m_selected = selected;
355HistoryEntry::geometry(
int y,
int width, TQFontMetrics f)
361 m_formated->setWidth(width - 2);
362 return TQRect(0, y, width, m_formated->height() + 21);
365void HistoryEntry::updateTime(
const TQTime &execTime) {
369HistoryEntry::~HistoryEntry()
373#include "kexiquerydesignersqlhistory.moc"