From: tweenk Date: Sun, 7 Dec 2008 00:44:56 +0000 (+0000) Subject: Change the way preferences are loaded to simplify unit testing X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a4ceb4c68d07fabde1ec5eed287f66b1c848f0e3;p=inkscape.git Change the way preferences are loaded to simplify unit testing --- diff --git a/build.xml b/build.xml index a143876c9..fee13acf3 100644 --- a/build.xml +++ b/build.xml @@ -213,6 +213,7 @@ + diff --git a/src/Makefile_insert b/src/Makefile_insert index 4bef3dc56..1d2ba9352 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -409,6 +409,7 @@ test_src_includes = \ $(srcdir)/extract-uri-test.h \ $(srcdir)/mod360-test.h \ $(srcdir)/round-test.h \ + $(srcdir)/preferences-test.h \ $(srcdir)/sp-gradient-test.h \ $(srcdir)/sp-style-elem-test.h \ $(srcdir)/style-test.h \ diff --git a/src/application/editor.cpp b/src/application/editor.cpp index 24c811778..c0501389f 100644 --- a/src/application/editor.cpp +++ b/src/application/editor.cpp @@ -65,7 +65,8 @@ Editor::Editor (gint /*argc*/, char **argv) sp_object_type_register ("sodipodi:namedview", SP_TYPE_NAMEDVIEW); sp_object_type_register ("sodipodi:guide", SP_TYPE_GUIDE); - Inkscape::Preferences::load(); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + prefs->load(true, false); } bool diff --git a/src/display/Makefile_insert b/src/display/Makefile_insert index 9ac9fd9cf..5994e19bd 100644 --- a/src/display/Makefile_insert +++ b/src/display/Makefile_insert @@ -129,7 +129,7 @@ display_libspdisplay_a_SOURCES = \ display/nr-light-types.h display_bezier_utils_test_SOURCES = display/bezier-utils-test.cpp -display_bezier_utils_test_LDADD = libnr/libnr.a -lglib-2.0 +display_bezier_utils_test_LDADD = libnr/libnr.a -lglib-2.0 2geom/lib2geom.a # Copy/paste from libnr display/test-display-main.cpp: display/test-display.cpp diff --git a/src/inkscape.cpp b/src/inkscape.cpp index 49b92394e..2a7f31c3e 100644 --- a/src/inkscape.cpp +++ b/src/inkscape.cpp @@ -750,9 +750,8 @@ inkscape_application_init (const gchar *argv0, gboolean use_gui) inkscape->argv0 = g_strdup(argv0); /* Load the preferences and menus; Later menu layout should be merged into prefs */ - Inkscape::Preferences::use_gui = use_gui; - Inkscape::Preferences::load(); Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + prefs->load(use_gui, false); inkscape_load_menus(inkscape); sp_input_load_from_preferences(); diff --git a/src/inkview.cpp b/src/inkview.cpp index 873cacc31..d19244911 100644 --- a/src/inkview.cpp +++ b/src/inkview.cpp @@ -210,8 +210,8 @@ main (int argc, const char **argv) LIBXML_TEST_VERSION Inkscape::GC::init(); - Inkscape::Preferences::use_gui = false; - Inkscape::Preferences::load(); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + prefs->load(false, true); // keep quiet about any failures gtk_init (&argc, (char ***) &argv); diff --git a/src/libnr/nr-compose-reference.cpp b/src/libnr/nr-compose-reference.cpp index b952939ed..b4ff5851a 100644 --- a/src/libnr/nr-compose-reference.cpp +++ b/src/libnr/nr-compose-reference.cpp @@ -19,19 +19,19 @@ static unsigned int pixelSize[] = { 1, 3, 4, 4 }; template static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha); -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { d[0] = DIV_ROUND((255*255 - alpha*s[3]) * d[0] + alpha*s[3]*s[0], 255*255); d[1] = DIV_ROUND((255*255 - alpha*s[3]) * d[1] + alpha*s[3]*s[1], 255*255); d[2] = DIV_ROUND((255*255 - alpha*s[3]) * d[2] + alpha*s[3]*s[2], 255*255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { d[0] = DIV_ROUND((255*255 - alpha*s[3]) * d[0] + 255*alpha*s[0], 255*255); d[1] = DIV_ROUND((255*255 - alpha*s[3]) * d[1] + 255*alpha*s[1], 255*255); d[2] = DIV_ROUND((255*255 - alpha*s[3]) * d[2] + 255*alpha*s[2], 255*255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { unsigned int newa = 255*255 - (255*255 - alpha*s[3]); d[0] = s[0];//newa == 0 ? 0 : DIV_ROUND(alpha*s[3]*s[0], newa); d[1] = s[1];//newa == 0 ? 0 : DIV_ROUND(alpha*s[3]*s[1], newa); @@ -39,7 +39,7 @@ template<> static void composePixel(unsigned char * d[3] = DIV_ROUND(newa, 255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { unsigned int newa = 255*255 - (255*255 - alpha*s[3]); d[0] = s[3] == 0 ? 0 : DIV_ROUND(255*s[0], s[3]);//newa == 0 ? 0 : DIV_ROUND(255*alpha*s[0], newa); d[1] = s[3] == 0 ? 0 : DIV_ROUND(255*s[1], s[3]);//newa == 0 ? 0 : DIV_ROUND(255*alpha*s[1], newa); @@ -47,7 +47,7 @@ template<> static void composePixel(unsigned char * d[3] = DIV_ROUND(newa, 255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { if ( d[3] == 0 ) { composePixel(d, s, alpha); } else if ( alpha*s[3] == 0 ) { @@ -61,7 +61,7 @@ template<> static void composePixel(unsigned ch } } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { if ( d[3] == 0 ) { composePixel(d, s, alpha); } else if ( alpha*s[3] == 0 ) { @@ -75,28 +75,28 @@ template<> static void composePixel(unsigned ch } } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { d[0] = DIV_ROUND(alpha*s[3]*s[0], 255*255); d[1] = DIV_ROUND(alpha*s[3]*s[1], 255*255); d[2] = DIV_ROUND(alpha*s[3]*s[2], 255*255); d[3] = DIV_ROUND(255*255 - (255*255 - alpha*s[3]), 255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { d[0] = DIV_ROUND(alpha*s[0], 255); d[1] = DIV_ROUND(alpha*s[1], 255); d[2] = DIV_ROUND(alpha*s[2], 255); d[3] = DIV_ROUND(255*255 - (255*255 - alpha*s[3]), 255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { d[0] = DIV_ROUND((255*255 - alpha*s[3]) * d[0] + alpha*s[3]*s[0], 255*255); d[1] = DIV_ROUND((255*255 - alpha*s[3]) * d[1] + alpha*s[3]*s[1], 255*255); d[2] = DIV_ROUND((255*255 - alpha*s[3]) * d[2] + alpha*s[3]*s[2], 255*255); d[3] = DIV_ROUND(255*255*255 - (255*255 - alpha*s[3]) * (255 - d[3]), 255*255); } -template<> static void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { +template<> void composePixel(unsigned char *d, const unsigned char *s, unsigned int alpha) { d[0] = DIV_ROUND((255*255 - alpha*s[3]) * d[0] + 255 * alpha*s[0], 255*255); d[1] = DIV_ROUND((255*255 - alpha*s[3]) * d[1] + 255 * alpha*s[1], 255*255); d[2] = DIV_ROUND((255*255 - alpha*s[3]) * d[2] + 255 * alpha*s[2], 255*255); diff --git a/src/libnr/nr-compose-test.h b/src/libnr/nr-compose-test.h index c40975920..fe3ccd61f 100644 --- a/src/libnr/nr-compose-test.h +++ b/src/libnr/nr-compose-test.h @@ -12,10 +12,10 @@ static inline unsigned int DIV_ROUND(unsigned int v, unsigned int divisor) { ret static inline unsigned char NR_PREMUL_111(unsigned int c, unsigned int a) { return static_cast(DIV_ROUND(c*a, 255)); } template -static int IMGCMP(const unsigned char* a, const unsigned char* b, size_t n) { return memcmp(a, b, n); } +int IMGCMP(const unsigned char* a, const unsigned char* b, size_t n) { return memcmp(a, b, n); } template<> -static int IMGCMP(const unsigned char* a, const unsigned char* b, size_t n) +int IMGCMP(const unsigned char* a, const unsigned char* b, size_t n) { // If two pixels each have their alpha channel set to zero they're equivalent // Note that this doesn't work for premultiplied values, as their color values should diff --git a/src/preferences.cpp b/src/preferences.cpp index 8b8289849..76edc2f7c 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -54,8 +54,11 @@ Preferences::Preferences() : _prefs_basename(PREFERENCES_FILE_NAME), _prefs_dir(""), _prefs_filename(""), - _writable(false), - _prefs_doc(NULL) + _prefs_doc(NULL), + _use_gui(true), + _quiet(false), + _loaded(false), + _writable(false) { // profile_path essentailly returns the argument prefixed by the profile directory. gchar *path = profile_path(NULL); @@ -66,7 +69,7 @@ Preferences::Preferences() : _prefs_filename = path; g_free(path); - _load(); + _loadDefaults(); } Preferences::~Preferences() @@ -99,12 +102,13 @@ void Preferences::_loadDefaults() * Tries to load the user's preferences.xml file. If there is none, creates it. * Displays dialog boxes on any errors. */ -void Preferences::_load() -{ - _loadDefaults(); - +void Preferences::load(bool use_gui, bool quiet) +{ Glib::ustring const not_saved = _("Inkscape will run with default settings, " "and new settings will not be saved. "); + _use_gui = use_gui; + _quiet = quiet; + _loaded = true; // NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose @@ -615,7 +619,8 @@ void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_ void Preferences::_errorDialog(Glib::ustring const &msg, Glib::ustring const &secondary) { - if (Preferences::use_gui) { + if (_quiet) return; + if (_use_gui) { Gtk::MessageDialog err( msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); err.set_secondary_text(secondary); @@ -631,7 +636,6 @@ Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &pa return Entry(path, ptr); } -bool Preferences::use_gui = true; Preferences *Preferences::_instance = NULL; diff --git a/src/preferences.h b/src/preferences.h index e39adc228..2f2337b81 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -196,10 +196,24 @@ public: // utility methods /** - * @name Save preferences to the disk. + * @name Load stored preferences and save them to the disk. * @{ */ + /** + * @brief Load the preferences from the default location. + * + * Loads the stored user preferences and enables saving them. If there's + * no preferences file in the expected location, it creates it. Any changes + * made to the preferences before loading will be overridden by the stored + * prefs. Not calling load() is sometimes useful, e.g. for testing. + * + * @param use_gui Whether to use dialogs to notify about errors when + * loading the preferences. Set to false in console mode. + * @param quiet Whether to output any messages about preference loading. + * If this is true, the use_gui parameter is ignored. + */ + void load(bool use_gui=true, bool quiet=false); /** * @brief Save all preferences to the hard disk. * @@ -372,6 +386,7 @@ public: /** * @name Access and manipulate the Preferences object. + * @{ */ /** @@ -381,15 +396,6 @@ public: if (!_instance) _instance = new Preferences(); return _instance; } - /** - * @brief Load the preferences. - * - * This method is automatically called from get(). It exists to supress - * possible compiler warnings over unused variables. - */ - static void load() { - if (!_instance) _instance = new Preferences(); - } /** * @brief Unload all preferences and store them on the hard disk. * @@ -402,14 +408,6 @@ public: _instance = NULL; } } - - /** - * @brief Whether to use GUI error notifications - * - * Set this to false when running Inkscape in command-line mode. - * Preference-related warnings will be printed to the console. - */ - static bool use_gui; /*@}*/ protected: @@ -428,7 +426,6 @@ protected: private: Preferences(); ~Preferences(); - void _load(); void _loadDefaults(); void _getRawValue(Glib::ustring const &path, gchar const *&result); void _setRawValue(Glib::ustring const &path, gchar const *value); @@ -444,8 +441,11 @@ private: std::string _prefs_basename; ///< Basename of the prefs file std::string _prefs_dir; ///< Directory in which to look for the prefs file std::string _prefs_filename; ///< Full filename (with directory) of the prefs file - bool _writable; ///< Will the preferences be saved at exit? XML::Document *_prefs_doc; ///< XML document storing all the preferences + bool _use_gui; ///< Use GUI error notifications? + bool _quiet; ///< Display any messages about loading? + bool _loaded; ///< Was a load attempt made? + bool _writable; ///< Will the preferences be saved at exit? /// Wrapper class for XML node observers class PrefNodeObserver; diff --git a/src/svg/Makefile_insert b/src/svg/Makefile_insert index 45a1a7db3..6a6c7bcd2 100644 --- a/src/svg/Makefile_insert +++ b/src/svg/Makefile_insert @@ -52,6 +52,8 @@ svg_test_svg_includes = \ svg_libtest_svg_a_SOURCES = \ svg/test-svg.cpp \ + preferences.cpp \ + sp-object.cpp \ $(svg_test_svg_includes) svg_libtest_stubs_a_SOURCES = \ @@ -62,9 +64,15 @@ svg_test_svg_SOURCES = \ $(svg_test_svg_includes) svg_test_svg_LDADD = \ - svg/libspsvg.a \ + svg/libspsvg.a \ svg/libtest-svg.a \ svg/libtest-stubs.a \ 2geom/lib2geom.a \ - libnr/libnr.a \ + libnr/libnr.a \ + xml/libspxml.a \ + util/libinkutil.a \ + io/libio.a \ + libinkpost.a \ + debug/libinkdebug.a \ + libcroco/libcroco.a \ $(INKSCAPE_LIBS) diff --git a/src/svg/svg-affine.cpp b/src/svg/svg-affine.cpp index 1077f7e2f..1ff9776e5 100644 --- a/src/svg/svg-affine.cpp +++ b/src/svg/svg-affine.cpp @@ -1,5 +1,3 @@ -#define __SP_SVG_AFFINE_C__ - /* * SVG data parser * @@ -251,4 +249,15 @@ gchar * sp_svg_transform_write(Geom::Matrix const *transform) { return sp_svg_transform_write(*transform); -} \ No newline at end of file +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :