From: Abdelrazak Younes Date: Thu, 5 Jun 2008 08:31:22 +0000 (+0000) Subject: Provide a generic way to retrieve icons from the frontend. X-Git-Tag: 1.6.10~4501 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=b1002fc32cc0d56fc42fd08781dbfd7c64fc60a6;p=features.git Provide a generic way to retrieve icons from the frontend. * 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 --- diff --git a/src/frontends/Application.h b/src/frontends/Application.h index 489e96e0a2..e6450e3c0e 100644 --- a/src/frontends/Application.h +++ b/src/frontends/Application.h @@ -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 diff --git a/src/frontends/qt4/GuiApplication.cpp b/src/frontends/qt4/GuiApplication.cpp index 732d25ff67..40b98452f5 100644 --- a/src/frontends/qt4/GuiApplication.cpp +++ b/src/frontends/qt4/GuiApplication.cpp @@ -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" @@ -63,11 +65,15 @@ #include #include #include +#include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -87,7 +93,6 @@ #endif #ifdef Q_WS_WIN -#include #include #if defined(Q_CYGWIN_WIN) || defined(Q_CC_MINGW) #include @@ -98,11 +103,22 @@ #include #include +#include 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; diff --git a/src/frontends/qt4/GuiApplication.h b/src/frontends/qt4/GuiApplication.h index 4387aa5702..f304fb9e5a 100644 --- a/src/frontends/qt4/GuiApplication.h +++ b/src/frontends/qt4/GuiApplication.h @@ -16,11 +16,12 @@ #include "frontends/Application.h" #include -#include #include -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 diff --git a/src/frontends/qt4/GuiToolbar.cpp b/src/frontends/qt4/GuiToolbar.cpp index 7ecd0b60ab..8cec104352 100644 --- a/src/frontends/qt4/GuiToolbar.cpp +++ b/src/frontends/qt4/GuiToolbar.cpp @@ -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 #include @@ -70,187 +68,12 @@ #include "support/lassert.h" -#include -#include - 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