Settings.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "Settings.h"
  2. #include <QApplication>
  3. static const char *DebuggerFontNames[Settings::FONT_END] = {
  4. "Application Font", "Default Fixed Font", "Code Font",
  5. "Label Font", "Hex viewer font"
  6. };
  7. static QString fontLocation(Settings::DebuggerFont f)
  8. {
  9. return QString("Fonts/").append(DebuggerFontNames[f]);
  10. }
  11. static QString fontColorLocation(Settings::DebuggerFont f)
  12. {
  13. return QString("Fonts/%1 Color").arg(DebuggerFontNames[f]);
  14. }
  15. Settings::Settings()
  16. : QSettings("openMSX", "debugger")
  17. {
  18. getFontsFromSettings();
  19. }
  20. Settings::~Settings()
  21. {
  22. }
  23. Settings& Settings::get()
  24. {
  25. static Settings instance;
  26. return instance;
  27. }
  28. void Settings::getFontsFromSettings()
  29. {
  30. // first get application default
  31. QVariant v = value(fontLocation(APP_FONT));
  32. if (v.type() == QVariant::Font) {
  33. fonts[APP_FONT] = v.value<QFont>();
  34. fontTypes[APP_FONT] = CUSTOM;
  35. } else {
  36. fontTypes[APP_FONT] = APPLICATION_DEFAULT;
  37. fonts[APP_FONT] = qApp->font();
  38. }
  39. // then get default fixed spacing font
  40. v = value(fontLocation(FIXED_FONT));
  41. if (v.type() == QVariant::Font) {
  42. fonts[FIXED_FONT] = v.value<QFont>();
  43. fontTypes[FIXED_FONT] = CUSTOM;
  44. } else {
  45. fontTypes[FIXED_FONT] = APPLICATION_DEFAULT;
  46. fonts[FIXED_FONT] = fonts[APP_FONT];
  47. }
  48. // then the rest
  49. for (int f = CODE_FONT; f < FONT_END; ++f) {
  50. v = value(fontLocation((DebuggerFont)f));
  51. if (v.type() == QVariant::Font) {
  52. fonts[f] = v.value<QFont>();
  53. fontTypes[f] = CUSTOM;
  54. } else if (v.toString() == "FixedDefault") {
  55. fonts[f] = fonts[FIXED_FONT];
  56. fontTypes[f] = FIXED_DEFAULT;
  57. } else {
  58. fonts[f] = fonts[APP_FONT];
  59. fontTypes[f] = CUSTOM;
  60. }
  61. }
  62. // read colors
  63. for (int f =CODE_FONT; f < FONT_END; ++f) {
  64. fontColors[f] =
  65. value(fontColorLocation((DebuggerFont)f), QColor(Qt::black))
  66. .value<QColor>();
  67. }
  68. }
  69. QString Settings::fontName(DebuggerFont f) const
  70. {
  71. return QString(DebuggerFontNames[f]);
  72. }
  73. const QFont& Settings::font(DebuggerFont f) const
  74. {
  75. return fonts[f];
  76. }
  77. void Settings::setFont(DebuggerFont f, const QFont& ft)
  78. {
  79. fontTypes[f] = CUSTOM;
  80. fonts[f] = ft;
  81. setValue(fontLocation(f), fonts[f]);
  82. if (f <= FIXED_FONT) updateFonts();
  83. }
  84. Settings::DebuggerFontType Settings::fontType(DebuggerFont f) const
  85. {
  86. return fontTypes[f];
  87. }
  88. void Settings::setFontType(DebuggerFont f, DebuggerFontType t)
  89. {
  90. if (fontTypes[f] == t) return;
  91. fontTypes[f] = t;
  92. switch (t) {
  93. case APPLICATION_DEFAULT:
  94. setValue(fontLocation(f), "AppDefault");
  95. if (f == APP_FONT) {
  96. fonts[f] = qApp->font();
  97. } else {
  98. fonts[f] = fonts[APP_FONT];
  99. }
  100. break;
  101. case FIXED_DEFAULT:
  102. if (f > FIXED_FONT) {
  103. setValue(fontLocation(f), "FixedDefault");
  104. fonts[f] = fonts[FIXED_FONT];
  105. }
  106. break;
  107. case CUSTOM:
  108. setValue(fontLocation(f), fonts[f]);
  109. break;
  110. }
  111. if (f > FIXED_FONT) updateFonts();
  112. }
  113. const QColor& Settings::fontColor(DebuggerFont f) const
  114. {
  115. return fontColors[f];
  116. }
  117. void Settings::setFontColor(DebuggerFont f, const QColor& c)
  118. {
  119. if (f > FIXED_FONT) {
  120. fontColors[f] = c;
  121. setValue(fontColorLocation(f), c);
  122. }
  123. }
  124. void Settings::updateFonts()
  125. {
  126. for (int f = CODE_FONT; f < FONT_END; ++f) {
  127. switch (fontTypes[f]) {
  128. case APPLICATION_DEFAULT:
  129. fonts[f] = fonts[APP_FONT];
  130. break;
  131. case FIXED_DEFAULT:
  132. fonts[f] = fonts[FIXED_FONT];
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. }