]> git.lyx.org Git - features.git/commitdiff
Provide a generic way to retrieve icons from the frontend.
authorAbdelrazak Younes <younes@lyx.org>
Thu, 5 Jun 2008 08:31:22 +0000 (08:31 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Thu, 5 Jun 2008 08:31:22 +0000 (08:31 +0000)
* Application: new iconName() pure virtual interface.

* GuiApplication: transfer icon searching code from GuiToolbar.cpp.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25139 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/Application.h
src/frontends/qt4/GuiApplication.cpp
src/frontends/qt4/GuiApplication.h
src/frontends/qt4/GuiToolbar.cpp

index 489e96e0a26256c471183ec04c4d94510613351c..e6450e3c0ea7cc2f21f539b5dde8aa18f0f7f26f 100644 (file)
@@ -224,6 +224,9 @@ public:
 
        virtual bool searchMenu(FuncRequest const & func,
                docstring_list & names) const = 0;
+
+       /// \return the icon file name for the given action.
+       virtual docstring iconName(FuncRequest const & f, bool unknown) = 0;
 };
 
 } // namespace frontend
index 732d25ff67290cd0abe38ca010355677b1b03033..40b98452f5aa6d6e40ed9327a81e254f9190c1a0 100644 (file)
@@ -39,6 +39,7 @@
 #include "Language.h"
 #include "Lexer.h"
 #include "LyX.h"
+#include "LyXAction.h"
 #include "LyXFunc.h"
 #include "LyXRC.h"
 #include "Session.h"
@@ -52,6 +53,7 @@
 #include "support/ForkedCalls.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/lyxalgo.h" // sorted
 #include "support/os.h"
 #include "support/Package.h"
 
 #include <QEventLoop>
 #include <QFileOpenEvent>
 #include <QHash>
+#include <QIcon>
 #include <QLocale>
 #include <QLibraryInfo>
+#include <QList>
 #include <QMacPasteboardMime>
 #include <QMenuBar>
 #include <QMimeData>
+#include <QObject>
+#include <QPixmap>
 #include <QPixmapCache>
 #include <QRegExp>
 #include <QSessionManager>
@@ -87,7 +93,6 @@
 #endif
 
 #ifdef Q_WS_WIN
-#include <QList>
 #include <QWindowsMime>
 #if defined(Q_CYGWIN_WIN) || defined(Q_CC_MINGW)
 #include <wtypes.h>
 #include <boost/bind.hpp>
 
 #include <exception>
+#include <vector>
 
 using namespace std;
 using namespace lyx::support;
 
 
+static void initializeResources()
+{
+       static bool initialized = false;
+       if (!initialized) {
+               Q_INIT_RESOURCE(Resources); 
+               initialized = true;
+       }
+}
+
+
 namespace lyx {
 
 frontend::Application * createApplication(int & argc, char * argv[])
@@ -112,6 +128,199 @@ frontend::Application * createApplication(int & argc, char * argv[])
 
 namespace frontend {
 
+////////////////////////////////////////////////////////////////////////
+// Icon loading support code.
+////////////////////////////////////////////////////////////////////////
+
+namespace {
+
+struct PngMap {
+       QString key;
+       QString value;
+};
+
+
+bool operator<(PngMap const & lhs, PngMap const & rhs)
+{
+       return lhs.key < rhs.key;
+}
+
+
+class CompareKey {
+public:
+       CompareKey(QString const & name) : name_(name) {}
+       bool operator()(PngMap const & other) const { return other.key == name_; }
+private:
+       QString const name_;
+};
+
+
+// this must be sorted alphabetically
+// Upper case comes before lower case
+PngMap sorted_png_map[] = {
+       { "Bumpeq", "bumpeq2" },
+       { "Cap", "cap2" },
+       { "Cup", "cup2" },
+       { "Delta", "delta2" },
+       { "Downarrow", "downarrow2" },
+       { "Gamma", "gamma2" },
+       { "Lambda", "lambda2" },
+       { "Leftarrow", "leftarrow2" },
+       { "Leftrightarrow", "leftrightarrow2" },
+       { "Longleftarrow", "longleftarrow2" },
+       { "Longleftrightarrow", "longleftrightarrow2" },
+       { "Longrightarrow", "longrightarrow2" },
+       { "Omega", "omega2" },
+       { "Phi", "phi2" },
+       { "Pi", "pi2" },
+       { "Psi", "psi2" },
+       { "Rightarrow", "rightarrow2" },
+       { "Sigma", "sigma2" },
+       { "Subset", "subset2" },
+       { "Supset", "supset2" },
+       { "Theta", "theta2" },
+       { "Uparrow", "uparrow2" },
+       { "Updownarrow", "updownarrow2" },
+       { "Upsilon", "upsilon2" },
+       { "Vdash", "vdash3" },
+       { "Vert", "vert2" },
+       { "Xi", "xi2" },
+       { "nLeftarrow", "nleftarrow2" },
+       { "nLeftrightarrow", "nleftrightarrow2" },
+       { "nRightarrow", "nrightarrow2" },
+       { "nVDash", "nvdash3" },
+       { "nvDash", "nvdash2" },
+       { "textrm \\AA", "textrm_AA"},
+       { "textrm \\O", "textrm_O"},
+       { "vDash", "vdash2" }
+};
+
+size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
+
+
+QString findPng(QString const & name)
+{
+       PngMap const * const begin = sorted_png_map;
+       PngMap const * const end = begin + nr_sorted_png_map;
+       LASSERT(sorted(begin, end), /**/);
+
+       PngMap const * const it = find_if(begin, end, CompareKey(name));
+
+       QString png_name;
+       if (it != end) {
+               png_name = it->value;
+       } else {
+               png_name = name;
+               png_name.replace('_', "underscore");
+               png_name.replace(' ', '_');
+
+               // This way we can have "math-delim { }" on the toolbar.
+               png_name.replace('(', "lparen");
+               png_name.replace(')', "rparen");
+               png_name.replace('[', "lbracket");
+               png_name.replace(']', "rbracket");
+               png_name.replace('{', "lbrace");
+               png_name.replace('}', "rbrace");
+               png_name.replace('|', "bars");
+               png_name.replace(',', "thinspace");
+               png_name.replace(':', "mediumspace");
+               png_name.replace(';', "thickspace");
+               png_name.replace('!', "negthinspace");
+       }
+
+       LYXERR(Debug::GUI, "findPng(" << name << ")\n"
+               << "Looking for math PNG called \"" << png_name << '"');
+       return png_name;
+}
+
+} // namespace anon
+
+
+QString iconName(FuncRequest const & f, bool unknown)
+{
+       initializeResources();
+       QString name1;
+       QString name2;
+       QString path;
+       switch (f.action) {
+       case LFUN_MATH_INSERT:
+               if (!f.argument().empty()) {
+                       path = "math/";
+                       name1 = findPng(toqstr(f.argument()).mid(1));
+               }
+               break;
+       case LFUN_MATH_DELIM:
+       case LFUN_MATH_BIGDELIM:
+               path = "math/";
+               name1 = findPng(toqstr(f.argument()));
+               break;
+       case LFUN_CALL:
+               path = "commands/";
+               name1 = toqstr(f.argument());
+               break;
+       default:
+               name2 = toqstr(lyxaction.getActionName(f.action));
+               name1 = name2;
+
+               if (!f.argument().empty()) {
+                       name1 = name2 + ' ' + toqstr(f.argument());
+                       name1.replace(' ', '_');
+               }
+       }
+
+       FileName fname = libFileSearch("images/" + path, name1, "png");
+       if (fname.exists())
+               return toqstr(fname.absFilename());
+
+       fname = libFileSearch("images/" + path, name2, "png");
+       if (fname.exists())
+               return toqstr(fname.absFilename());
+
+       path = ":/images/" + path;
+       QDir res(path);
+       if (!res.exists()) {
+               LYXERR0("Directory " << path << " not found in resource!"); 
+               return QString();
+       }
+       name1 += ".png";
+       if (res.exists(name1))
+               return path + name1;
+
+       name2 += ".png";
+       if (res.exists(name2))
+               return path + name2;
+
+       LYXERR(Debug::GUI, "Cannot find icon for command \""
+                          << lyxaction.getActionName(f.action)
+                          << '(' << to_utf8(f.argument()) << ")\"");
+
+       if (unknown)
+               return QString(":/images/unknown.png");
+
+       return QString();
+}
+
+
+QIcon getIcon(FuncRequest const & f, bool unknown)
+{
+       QString icon = iconName(f, unknown);
+       if (icon.isEmpty())
+               return QIcon();
+
+       LYXERR(Debug::GUI, "Found icon: " << icon);
+       QPixmap pm;
+       if (!pm.load(icon)) {
+               LYXERR0("Cannot load icon " << icon << " please verify resource system!");
+               return QIcon();
+       }
+
+       return QIcon(pm);
+}
+
+
+////////////////////////////////////////////////////////////////////////
+// LyX server support code.
+////////////////////////////////////////////////////////////////////////
 class SocketNotifier : public QSocketNotifier
 {
 public:
@@ -128,6 +337,7 @@ public:
 
 ////////////////////////////////////////////////////////////////////////
 // Mac specific stuff goes here...
+////////////////////////////////////////////////////////////////////////
 
 class MenuTranslator : public QTranslator
 {
@@ -492,6 +702,13 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
 }
 
 
+docstring GuiApplication::iconName(FuncRequest const & f, bool unknown)
+{
+       return qstring_to_ucs4(lyx::frontend::iconName(f, unknown));
+}
+
+
+
 bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 {
        bool enable = true;
index 4387aa570289b55b713bb74ef703569388ff6ed2..f304fb9e5abc0a4a9b972f89e4c3ac4a51832d1f 100644 (file)
 #include "frontends/Application.h"
 
 #include <QApplication>
-#include <QObject>
 #include <QList>
 
-class QSessionManager;
 class QAbstractItemModel;
+class QIcon;
+class QObject;
+class QSessionManager;
 class QSortFilterProxyModel;
 
 namespace lyx {
@@ -68,6 +69,7 @@ public:
        void registerSocketCallback(int fd, SocketCallback func);
        void unregisterSocketCallback(int fd);
        bool searchMenu(FuncRequest const & func, docstring_list & names) const;
+       docstring iconName(FuncRequest const & f, bool unknown);
        void hideDialogs(std::string const & name, Inset * inset) const;
        Buffer const * updateInset(Inset const * inset) const;
        //@}
@@ -142,6 +144,12 @@ private:
 
 extern GuiApplication * guiApp;
 
+/// \return the icon file name for the given action.
+QString iconName(FuncRequest const & f, bool unknown);
+
+/// \return an icon for the given action.
+QIcon getIcon(FuncRequest const & f, bool unknown);
+
 } // namespace frontend
 } // namespace lyx
 
index 7ecd0b60ab5c53390ba751fafed5c9bae68dfce5..8cec1043521ed102412e672b7bbb0a5d372c2235 100644 (file)
@@ -33,7 +33,6 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "Layout.h"
-#include "LyXAction.h"
 #include "LyXFunc.h"
 #include "LyXRC.h"
 #include "Paragraph.h"
@@ -43,7 +42,6 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/lyxalgo.h" // sorted
 
 #include <QAbstractItemDelegate>
 #include <QAbstractTextDocumentLayout>
 
 #include "support/lassert.h"
 
-#include <map>
-#include <vector>
-
 using namespace std;
 using namespace lyx::support;
 
-static void initializeResources()
-{
-       static bool initialized = false;
-       if (!initialized) {
-               Q_INIT_RESOURCE(Resources); 
-               initialized = true;
-       }
-}
-
-
 namespace lyx {
 namespace frontend {
 
-namespace {
-
-struct PngMap {
-       QString key;
-       QString value;
-};
-
-
-bool operator<(PngMap const & lhs, PngMap const & rhs)
-{
-               return lhs.key < rhs.key;
-}
-
-
-class CompareKey {
-public:
-       CompareKey(QString const & name) : name_(name) {}
-       bool operator()(PngMap const & other) const { return other.key == name_; }
-private:
-       QString const name_;
-};
-
-
-// this must be sorted alphabetically
-// Upper case comes before lower case
-PngMap sorted_png_map[] = {
-       { "Bumpeq", "bumpeq2" },
-       { "Cap", "cap2" },
-       { "Cup", "cup2" },
-       { "Delta", "delta2" },
-       { "Downarrow", "downarrow2" },
-       { "Gamma", "gamma2" },
-       { "Lambda", "lambda2" },
-       { "Leftarrow", "leftarrow2" },
-       { "Leftrightarrow", "leftrightarrow2" },
-       { "Longleftarrow", "longleftarrow2" },
-       { "Longleftrightarrow", "longleftrightarrow2" },
-       { "Longrightarrow", "longrightarrow2" },
-       { "Omega", "omega2" },
-       { "Phi", "phi2" },
-       { "Pi", "pi2" },
-       { "Psi", "psi2" },
-       { "Rightarrow", "rightarrow2" },
-       { "Sigma", "sigma2" },
-       { "Subset", "subset2" },
-       { "Supset", "supset2" },
-       { "Theta", "theta2" },
-       { "Uparrow", "uparrow2" },
-       { "Updownarrow", "updownarrow2" },
-       { "Upsilon", "upsilon2" },
-       { "Vdash", "vdash3" },
-       { "Vert", "vert2" },
-       { "Xi", "xi2" },
-       { "nLeftarrow", "nleftarrow2" },
-       { "nLeftrightarrow", "nleftrightarrow2" },
-       { "nRightarrow", "nrightarrow2" },
-       { "nVDash", "nvdash3" },
-       { "nvDash", "nvdash2" },
-       { "textrm \\AA", "textrm_AA"},
-       { "textrm \\O", "textrm_O"},
-       { "vDash", "vdash2" }
-};
-
-size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
-
-
-QString findPng(QString const & name)
-{
-       PngMap const * const begin = sorted_png_map;
-       PngMap const * const end = begin + nr_sorted_png_map;
-       LASSERT(sorted(begin, end), /**/);
-
-       PngMap const * const it = find_if(begin, end, CompareKey(name));
-
-       QString png_name;
-       if (it != end) {
-               png_name = it->value;
-       } else {
-               png_name = name;
-               png_name.replace('_', "underscore");
-               png_name.replace(' ', '_');
-
-               // This way we can have "math-delim { }" on the toolbar.
-               png_name.replace('(', "lparen");
-               png_name.replace(')', "rparen");
-               png_name.replace('[', "lbracket");
-               png_name.replace(']', "rbracket");
-               png_name.replace('{', "lbrace");
-               png_name.replace('}', "rbrace");
-               png_name.replace('|', "bars");
-               png_name.replace(',', "thinspace");
-               png_name.replace(':', "mediumspace");
-               png_name.replace(';', "thickspace");
-               png_name.replace('!', "negthinspace");
-       }
-
-       LYXERR(Debug::GUI, "findPng(" << name << ")\n"
-               << "Looking for math PNG called \"" << png_name << '"');
-       return png_name;
-}
-
-} // namespace anon
-
-
-/// return a icon for the given action
-static QIcon getIcon(FuncRequest const & f, bool unknown)
-{
-       initializeResources();
-       QPixmap pm;
-       QString name1;
-       QString name2;
-       QString path;
-       switch (f.action) {
-       case LFUN_MATH_INSERT:
-               if (!f.argument().empty()) {
-                       path = "math/";
-                       name1 = findPng(toqstr(f.argument()).mid(1));
-               }
-               break;
-       case LFUN_MATH_DELIM:
-       case LFUN_MATH_BIGDELIM:
-               path = "math/";
-               name1 = findPng(toqstr(f.argument()));
-               break;
-       case LFUN_CALL:
-               path = "commands/";
-               name1 = toqstr(f.argument());
-               break;
-       default:
-               name2 = toqstr(lyxaction.getActionName(f.action));
-               name1 = name2;
-
-               if (!f.argument().empty()) {
-                       name1 = name2 + ' ' + toqstr(f.argument());
-                       name1.replace(' ', '_');
-               }
-       }
-
-       string fullname = libFileSearch("images/" + path, name1, "png").absFilename();
-       if (pm.load(toqstr(fullname)))
-               return pm;
-
-       fullname = libFileSearch("images/" + path, name2, "png").absFilename();
-       if (pm.load(toqstr(fullname)))
-               return pm;
-
-       if (pm.load(":/images/" + path + name1 + ".png"))
-               return pm;
-
-       if (pm.load(":/images/" + path + name2 + ".png"))
-               return pm;
-
-       LYXERR(Debug::GUI, "Cannot find icon for command \""
-                          << lyxaction.getActionName(f.action)
-                          << '(' << to_utf8(f.argument()) << ")\"");
-       if (unknown)
-               pm.load(":/images/unknown.png");
-
-       return pm;
-}
-
-
 /////////////////////////////////////////////////////////////////////
 //
 // GuiLayoutBox