[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfenumlistwidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfenumlistwidget.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 #include <QUrl>
00025 #include <QUrlQuery>
00026 
00027 #include "klfenumlistwidget.h"
00028 
00029 
00030 KLFEnumListWidget::KLFEnumListWidget(QWidget *parent)
00031   : QLabel(QString(), parent)
00032 {
00033   setTextFormat(Qt::RichText);
00034   setWordWrap(true);
00035 
00036   connect(this, SIGNAL(linkActivated(const QString&)), this, SLOT(labelActionLink(const QString&)));
00037 }
00038 
00039 KLFEnumListWidget::~KLFEnumListWidget()
00040 {
00041 }
00042 
00043 QString KLFEnumListWidget::itemAt(int i) const
00044 {
00045   if (i >= 0 && i < pItems.size())
00046     return pItems[i].s;
00047   return QString();
00048 }
00049 
00050 QVariant KLFEnumListWidget::itemDataAt(int i) const
00051 {
00052   if (i >= 0 && i < pItems.size())
00053     return pItems[i].data;
00054   return QVariant();
00055 }
00056 
00057 QStringList KLFEnumListWidget::itemList() const
00058 {
00059   QStringList l;
00060   int k;
00061   for (k = 0; k < pItems.size(); ++k)
00062     l << pItems[k].s;
00063   return l;
00064 }
00065 QVariantList KLFEnumListWidget::itemDataList() const
00066 {
00067   QVariantList l;
00068   int k;
00069   for (k = 0; k < pItems.size(); ++k)
00070     l << pItems[k].data;
00071   return l;
00072 }
00073 
00074 
00075 void KLFEnumListWidget::removeItem(int i)
00076 {
00077   i = simple_wrapped_item_index(i);
00078   if (i < 0 || i >= pItems.size())
00079     return;
00080 
00081   pItems.removeAt(i);
00082 
00083   updateLabelText();
00084 }
00085 void KLFEnumListWidget::insertItem(int i, const QString& s, const QVariant& data)
00086 {
00087   i = simple_wrapped_item_index(i);
00088   pItems.insert(i, Item(s, data));
00089 
00090   updateLabelText();
00091 }
00092 
00093 void KLFEnumListWidget::setItems(const QStringList& slist, const QVariantList& datalist)
00094 {
00095   pItems.clear();
00096   int k;
00097   for (k = 0; k < slist.size(); ++k) {
00098     QVariant d;
00099     if (k < datalist.size())
00100       d = datalist[k];
00101     pItems << Item(slist[k], d);
00102   }
00103   if (k < datalist.size()) {
00104     qWarning()<<KLF_FUNC_NAME<<": Ignoring superfluous elements in data QVariantList";
00105   }
00106 
00107   updateLabelText();
00108 }
00109 
00110 void KLFEnumListWidget::updateLabelText()
00111 {
00112   QString t;
00113   t = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
00114     "<html><head><meta name=\"qrichtext\" content=\"1\" />\n"
00115     "<style type=\"text/css\">\n"
00116     ".item { white-space: nowrap }\n"
00117     "a.itemtext { color: black; text-decoration: underline; }\n"
00118     "a.actionlink { color: blue; text-decoration: none; font-weight: bold; }\n"
00119     "</style></head><body>\n"
00120     "<p>";
00121   int k;
00122   for (k = 0; k < pItems.size(); ++k) {
00123     t += "<span class=\"item\"><a class=\"itemtext\" href=\"klfenumlistwidgetaction:/itemClick?i="
00124       +QString::number(k)+"\">" + (pItems[k].s).toHtmlEscaped() + "</a>&nbsp;";
00125     t += "<a class=\"actionlink\" href=\"klfenumlistwidgetaction:/removeAt?i="+QString::number(k)+"\">[-]</a>"
00126       + "&nbsp;&nbsp;</span> ";
00127   }
00128   t += "</p>"   "</body></html>";
00129 
00130   setText(t);
00131 }
00132 
00133 void KLFEnumListWidget::labelActionLink(const QString& link)
00134 {
00135   QUrl url = QUrl::fromEncoded(link.toLatin1());
00136 
00137   klfDbg("link clicked="<<link<<" scheme="<<url.scheme()<<", path="<<url.path()) ;
00138 
00139   if (url.scheme() == "klfenumlistwidgetaction") {
00140     QUrlQuery q(url);
00141     if (url.path() == "/removeAt") {
00142       int i = q.queryItemValue("i").toInt();
00143       removeItem(i);
00144       return;
00145     }
00146     if (url.path() == "/itemClick") {
00147       int i = q.queryItemValue("i").toInt();
00148       emit itemActivated(i, itemAt(i), itemDataAt(i));
00149       emit itemActivated(itemAt(i), itemDataAt(i));
00150       return;
00151     }
00152   }
00153 
00154   klfDbg("don't know what to do with link: "<<link<<" ...") ;
00155 }
00156 
00157 
00158 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net