Browse Source

implement fonts and colors

Sonnix 6 years ago
parent
commit
a8536c750b
11 changed files with 146 additions and 120 deletions
  1. 5 5
      callbacks_gui.cpp
  2. 45 34
      mainwindow.cpp
  3. 5 3
      mainwindow.h
  4. 3 3
      qspimgcanvas.cpp
  5. 1 1
      qspimgcanvas.h
  6. 24 29
      qsplistbox.cpp
  7. 6 7
      qsplistbox.h
  8. 11 6
      qspmsgdlg.cpp
  9. 1 1
      qspmsgdlg.h
  10. 37 25
      qsptextbox.cpp
  11. 8 6
      qsptextbox.h

+ 5 - 5
callbacks_gui.cpp

@@ -147,9 +147,9 @@ void QSPCallBacks::RefreshInt(QSP_BOOL isRedraw)
 	else
         m_frame->GetDesc()->LoadBackImage(QString(""));
     // -------------------------------
+    m_frame->ApplyParams();
 	if (isRedraw)
 	{
-		m_frame->ApplyParams(); //Moved to inside avoid constant update?
 		m_frame->EnableControls(false, true);
         //m_frame->Update();
         QCoreApplication::processEvents();
@@ -288,8 +288,8 @@ void QSPCallBacks::Msg(const QSP_CHAR *str)
 {
 	if (m_frame->IsQuit()) return;
 	RefreshInt(QSP_FALSE);
-    QspMsgDlg dialog(m_frame->GetDesc()->GetBackgroundColour(),
-		m_frame->GetDesc()->GetForegroundColour(),
+    QspMsgDlg dialog(m_frame->GetDesc()->GetBackgroundColor(),
+        m_frame->GetDesc()->GetForegroundColor(),
 		m_frame->GetDesc()->GetTextFont(),
         "Info", //caption
         QSPTools::qspStrToQt(str),
@@ -329,8 +329,8 @@ void QSPCallBacks::Input(const QSP_CHAR *text, QSP_CHAR *buffer, int maxLen)
 	RefreshInt(QSP_FALSE);
 //	QSPInputDlg dialog(m_frame,
 //		wxID_ANY,
-//		m_frame->GetDesc()->GetBackgroundColour(),
-//		m_frame->GetDesc()->GetForegroundColour(),
+//		m_frame->GetDesc()->GetBackgroundColor(),
+//		m_frame->GetDesc()->GetForegroundColor(),
 //		m_frame->GetDesc()->GetTextFont(),
 //		_("Input data"),
 //		wxString(text.Str, text.End),

+ 45 - 34
mainwindow.cpp

@@ -8,6 +8,7 @@
 #include <QThread>
 #include <QCursor>
 #include <QPalette>
+#include <QFontDialog>
 
 #include "callbacks_gui.h"
 #include "comtools.h"
@@ -54,6 +55,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
     m_fontColor = palette().color(QPalette::Text);
     m_backColor = palette().color(QPalette::Window);
 
+    m_font = font();
+    m_isUseFontSize = false;
+    m_fontSize = m_font.pointSize();
+
     m_imgView = new QspImgCanvas(this);
 
     CreateDockWindows();
@@ -89,49 +94,46 @@ void MainWindow::ApplyParams()
     int numVal;
     QSP_CHAR *strVal;
     QColor setBackColor, setFontColor, setLinkColor;
-//	wxString setFontName;
-//	int setFontSize;
     bool isRefresh = false;
     // --------------
     setBackColor = ((QSPGetVarValues(QSP_FMT("BCOLOR"), 0, &numVal, &strVal) && numVal) ? QColor::fromRgba(numVal) : m_backColor);
-    if (setBackColor != _mainDescTextBox->GetBackgroundColour())
+    if (setBackColor != m_backColor)
     {
         if (ApplyBackColor(setBackColor)) isRefresh = true;
     }
     // --------------
     setFontColor = ((QSPGetVarValues(QSP_FMT("FCOLOR"), 0, &numVal, &strVal) && numVal) ? QColor::fromRgba(numVal) : m_fontColor);
-    if (setFontColor != _mainDescTextBox->GetForegroundColour())
+    if (setFontColor != m_fontColor)
     {
         if (ApplyFontColor(setFontColor)) isRefresh = true;
     }
     // --------------
     setLinkColor = ((QSPGetVarValues(QSP_FMT("LCOLOR"), 0, &numVal, &strVal) && numVal) ? QColor::fromRgba(numVal) : m_linkColor);
-    if (setLinkColor != _mainDescTextBox->GetLinkColor())
+    if (setLinkColor != m_linkColor)
     {
         if (ApplyLinkColor(setLinkColor)) isRefresh = true;
     }
     // --------------
-//	if (m_isUseFontSize)
-//		setFontSize = m_fontSize;
-//	else
-//		setFontSize = ((QSPGetVarValues(QSP_FMT("FSIZE"), 0, &numVal, &strVal) && numVal) ? numVal : m_fontSize);
-//	if (setFontSize != m_desc->GetTextFont().GetPointSize())
-//	{
-//		if (ApplyFontSize(setFontSize)) isRefresh = true;
-//	}
-//	// --------------
-//	setFontName = ((QSPGetVarValues(QSP_FMT("FNAME"), 0, &numVal, &strVal) && strVal && *strVal) ? wxString(strVal) : m_fontName);
-//	if (!setFontName.IsSameAs(m_desc->GetTextFont().GetFaceName(), false))
-//	{
-//		if (ApplyFontName(setFontName))
-//			isRefresh = true;
-//		else if (!m_fontName.IsSameAs(m_desc->GetTextFont().GetFaceName(), false))
-//		{
-//			if (ApplyFontName(m_fontName)) isRefresh = true;
-//		}
-//	}
-//	// --------------
-    if (isRefresh) RefreshUI();
+    QFont new_font = m_font;
+    if(QSPGetVarValues(QSP_FMT("FNAME"), 0, &numVal, &strVal))
+        if(strVal != 0)
+        {
+            new_font.setFamily(QSPTools::qspStrToQt(strVal));
+        }
+    if(!m_isUseFontSize)
+    {
+        if(QSPGetVarValues(QSP_FMT("FSIZE"), 0, &numVal, &strVal))
+            if(numVal != 0)
+            {
+                new_font.setPointSize(numVal);
+            }
+    }
+    if(new_font != m_font)
+    {
+        ApplyFont(new_font);
+    }
+    // --------------
+    //if (isRefresh) RefreshUI();
 }
 
 void MainWindow::DeleteMenu()
@@ -234,13 +236,22 @@ void MainWindow::RefreshUI()
     m_imgView->RefreshUI();
 }
 
+void MainWindow::ApplyFont(const QFont &new_font)
+{
+    m_font = new_font;
+    _mainDescTextBox->SetTextFont(new_font);
+    _descTextBox->SetTextFont(new_font);
+    _objectsListBox->SetTextFont(new_font);
+    _actionsListBox->SetTextFont(new_font);
+}
+
 bool MainWindow::ApplyFontColor(const QColor &color)
 {
     m_fontColor = color;
-    _mainDescTextBox->SetForegroundColour(color);
-    _descTextBox->SetForegroundColour(color);
-    _objectsListBox->SetForegroundColour(color);
-    _actionsListBox->SetForegroundColour(color);
+    _mainDescTextBox->SetForegroundColor(color);
+    _descTextBox->SetForegroundColor(color);
+    _objectsListBox->SetForegroundColor(color);
+    _actionsListBox->SetForegroundColor(color);
     return false;
 }
 
@@ -250,10 +261,10 @@ bool MainWindow::ApplyBackColor(const QColor &color)
     QPalette p = palette();
     p.setColor(QPalette::Base, color);
     setPalette(p);
-    _mainDescTextBox->SetBackgroundColour(color);
-    _descTextBox->SetBackgroundColour(color);
-    _objectsListBox->SetBackgroundColour(color);
-    _actionsListBox->SetBackgroundColour(color);
+    _mainDescTextBox->SetBackgroundColor(color);
+    _descTextBox->SetBackgroundColor(color);
+    _objectsListBox->SetBackgroundColor(color);
+    _actionsListBox->SetBackgroundColor(color);
     return false;
 }
 

+ 5 - 3
mainwindow.h

@@ -12,6 +12,7 @@
 #include <QAction>
 #include <QToolBar>
 #include <QStatusBar>
+#include <QFont>
 
 #include "qsptextbox.h"
 #include "qsplistbox.h"
@@ -70,8 +71,8 @@ public:
     bool IsKeyPressedWhileDisabled() const { return m_keyPressedWhileDisabled; }
 
     QColor GetLinkColor() { return m_linkColor; }
-    QColor GetBackgroundColour() { return m_backColor; }
-    QColor GetForegroundColour() { return m_fontColor; }
+    QColor GetBackgroundColor() { return m_backColor; }
+    QColor GetForegroundColor() { return m_fontColor; }
 
     void SetShowPlainText(bool isPlain);
 
@@ -87,7 +88,7 @@ private:
     void UpdateTitle();
     void ReCreateGUI();
     void RefreshUI();
-//    void ApplyFont(const wxFont& font);
+    void ApplyFont(const QFont& new_font);
 //    bool ApplyFontSize(int size);
 //    bool ApplyFontName(const wxString& name);
     bool ApplyFontColor(const QColor& color);
@@ -127,6 +128,7 @@ private:
     QColor m_fontColor;
     int m_fontSize;
     QString m_fontName;
+    QFont m_font;
     bool m_isUseFontSize;
     bool m_isProcessEvents;
     bool m_isQuit;

+ 3 - 3
qspimgcanvas.cpp

@@ -76,10 +76,10 @@ void QspImgCanvas::RefreshUI()
     //		Refresh();
 }
 
-bool QspImgCanvas::SetBackgroundColour(const QColor &color)
+bool QspImgCanvas::SetBackgroundColor(const QColor &color)
 {
-    //wxWindow::SetBackgroundColour(color);
-    //m_animation->SetBackgroundColour(color);
+    //wxWindow::SetBackgroundColor(color);
+    //m_animation->SetBackgroundColor(color);
     return true;
 }
 

+ 1 - 1
qspimgcanvas.h

@@ -28,7 +28,7 @@ public:
     void RefreshUI();
 
     // Overloaded methods
-    virtual bool SetBackgroundColour(const QColor& color);
+    virtual bool SetBackgroundColor(const QColor& color);
 
 private:
     // Fields

+ 24 - 29
qsplistbox.cpp

@@ -20,7 +20,7 @@ QspListBox::QspListBox(QWidget *parent) : QListWidget(parent)
     m_linkColor = palette().color(QPalette::Link);
     m_textColor = palette().color(QPalette::Text);
     m_backgroundColor = palette().color(QPalette::Window);
-//	SetStandardFonts(m_font.GetPointSize(), fontName, fontName);
+    m_font = font();
 }
 
 QspListBox::~QspListBox()
@@ -28,11 +28,6 @@ QspListBox::~QspListBox()
 
 }
 
-void QspListBox::SetStandardFonts(const QFont &font)
-{
-    //	RefreshUI();
-}
-
 void QspListBox::RefreshUI()
 {
 //	RefreshAll();
@@ -85,22 +80,21 @@ void QspListBox::SetIsShowNums(bool isShow)
     }
 }
 
-void QspListBox::SetTextFont(const QFont& font)
+void QspListBox::SetTextFont(const QFont& new_font)
 {
-//	int fontSize = font.GetPointSize();
-//	wxString fontName(font.GetFaceName());
-//	if (!m_font.GetFaceName().IsSameAs(fontName, false) || m_font.GetPointSize() != fontSize)
-//	{
-//		m_font = font;
-//		SetStandardFonts(fontSize, fontName, fontName);
-//	}
+    if (m_font != new_font)
+    {
+        m_font = new_font;
+        setFont(new_font);
+        createList();
+    }
 }
 
-bool QspListBox::SetLinkColor(const QColor &colour)
+bool QspListBox::SetLinkColor(const QColor &color)
 {
-    if(m_linkColor != colour)
+    if(m_linkColor != color)
     {
-        m_linkColor = colour;
+        m_linkColor = color;
         createList();
         //RefreshUI();
         return true;
@@ -113,32 +107,32 @@ QColor QspListBox::GetLinkColor()
     return m_linkColor;
 }
 
-QColor QspListBox::GetBackgroundColour()
+QColor QspListBox::GetBackgroundColor()
 {
     return m_backgroundColor;
 }
 
-QColor QspListBox::GetForegroundColour()
+QColor QspListBox::GetForegroundColor()
 {
     return m_textColor;
 }
 
-bool QspListBox::SetBackgroundColour(const QColor &colour)
+bool QspListBox::SetBackgroundColor(const QColor &color)
 {
-    if(m_backgroundColor != colour)
+    if(m_backgroundColor != color)
     {
-        m_backgroundColor = colour;
+        m_backgroundColor = color;
         createList();
         return true;
     }
     return false;
 }
 
-bool QspListBox::SetForegroundColour(const QColor &colour)
+bool QspListBox::SetForegroundColor(const QColor &color)
 {
-    if(m_textColor != colour)
+    if(m_textColor != color)
     {
-        m_textColor = colour;
+        m_textColor = color;
         createList();
         return true;
     }
@@ -206,8 +200,9 @@ void QspListBox::createList()
         item_widget->SetShowPlainText(showPlainText);
 
         item_widget->SetLinkColor(m_linkColor);
-        item_widget->SetBackgroundColour(m_backgroundColor);
-        item_widget->SetForegroundColour(m_textColor);
+        item_widget->SetBackgroundColor(m_backgroundColor);
+        item_widget->SetForegroundColor(m_textColor);
+        item_widget->SetTextFont(m_font);
 
         item_widget->SetGamePath(m_path);
         //item_widget->setMaximumHeight(800);
@@ -219,7 +214,7 @@ void QspListBox::createList()
         item_widget->setBackgroundRole(QPalette::NoRole);
         item_widget->setTextInteractionFlags(Qt::NoTextInteraction);
         item_widget->setWordWrapMode(QTextOption::NoWrap);
-        QFontMetrics font_metrics(item_widget->font());
+        //QFontMetrics font_metrics(item_widget->font());
         //item_widget->setFixedHeight(font_metrics.height() + 4* item_widget->frameWidth());
         item_widget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
         item_widget->sizePolicy().setHorizontalPolicy(QSizePolicy::Expanding);
@@ -257,7 +252,7 @@ QString QspListBox::formatItem(int itemIndex)
             isImage = true;
         }
     }
-    QString color(QSPTools::GetHexColor(GetForegroundColour()));
+    QString color(QSPTools::GetHexColor(GetForegroundColor()));
     QString formatedText;
     if(isImage)
     {

+ 6 - 7
qsplistbox.h

@@ -21,7 +21,6 @@ public:
     ~QspListBox();
 
     // Methods
-    void SetStandardFonts(const QFont& font);
     void RefreshUI();
     void BeginItems();
     void AddItem(const QString& image, const QString& desc);
@@ -30,14 +29,14 @@ public:
     // Accessors
     void SetIsHtml(bool isHtml);
     void SetIsShowNums(bool isShow);
-    void SetTextFont(const QFont& font);
+    void SetTextFont(const QFont& new_font);
     QFont GetTextFont() const { return m_font; }
-    bool SetLinkColor(const QColor &colour);
+    bool SetLinkColor(const QColor &color);
     QColor GetLinkColor();
-    QColor GetBackgroundColour();
-    QColor GetForegroundColour(); //text color
-    bool SetBackgroundColour(const QColor& colour);
-    bool SetForegroundColour(const QColor& colour);
+    QColor GetBackgroundColor();
+    QColor GetForegroundColor(); //text color
+    bool SetBackgroundColor(const QColor& color);
+    bool SetForegroundColor(const QColor& color);
     void SetGamePath(const QString& path) { m_path = path; }
     void SetSelection(int selection);
     void SetShowPlainText(bool isPlain);

+ 11 - 6
qspmsgdlg.cpp

@@ -1,6 +1,7 @@
 #include "qspmsgdlg.h"
 
 #include <QRect>
+#include <QPalette>
 
 #include "mainwindow.h"
 
@@ -38,20 +39,24 @@ QspMsgDlg::QspMsgDlg(const QString &caption, const QString &text, QWidget *paren
 
 QspMsgDlg::QspMsgDlg(const QColor& backColor,
                      const QColor& fontColor,
-                     const QFont& font,
+                     const QFont& new_font,
                      const QString& caption,
                      const QString& text,
                      bool isHtml,
                      const QString& gamePath,
                      QWidget *parent) : QDialog(parent)
 {
-    //SetBackgroundColour(backColor);
     m_desc.SetGamePath(gamePath);
     m_desc.SetIsHtml(isHtml);
-    m_desc.SetBackgroundColour(backColor);
-    m_desc.SetForegroundColour(fontColor);
-    m_desc.SetTextFont(font);
+    m_desc.SetBackgroundColor(backColor);
+    m_desc.SetForegroundColor(fontColor);
+    m_desc.SetTextFont(new_font);
     m_desc.SetText(text);
+    m_desc.setBackgroundRole(QPalette::NoRole);
+    QPalette p = palette();
+    p.setColor(QPalette::Base, backColor);
+    //p.setColor(QPalette::Window, backColor);
+    setPalette(p);
     //#ifdef __WXMSW__
     //	btnOk->SetBackgroundColour(backColor);
     //	btnOk->SetForegroundColour(fontColor);
@@ -66,7 +71,7 @@ QspMsgDlg::QspMsgDlg(const QColor& backColor,
     okButton.setGeometry(QRect(10, 130, 100, 20));
     okButton.setText("OK");
     okButton.setDefault(true);
-    okButton.setFont(font);
+    okButton.setFont(new_font);
     okButton.setFocus(Qt::PopupFocusReason);
 
     resize(450,100);

+ 1 - 1
qspmsgdlg.h

@@ -24,7 +24,7 @@ public:
     QspMsgDlg(const QString &caption = "", const QString &text = "", QWidget *parent = 0);
     QspMsgDlg(const QColor& backColor,
               const QColor& fontColor,
-              const QFont& font,
+              const QFont& new_font,
               const QString& caption,
               const QString& text,
               bool isHtml,

+ 37 - 25
qsptextbox.cpp

@@ -13,6 +13,9 @@ QspTextBox::QspTextBox(QWidget *parent) : QTextBrowser(parent)
     m_isUseHtml = false;
     showPlainText = false;
     m_linkColor = palette().color(QPalette::Link);
+    m_fontColor = palette().color(QPalette::Text);
+    m_backColor = palette().color(QPalette::Window);
+    m_font = font();
     setOpenLinks(false);
 //	m_font = *wxNORMAL_FONT;
 //	m_outFormat = wxString::Format(
@@ -20,8 +23,6 @@ QspTextBox::QspTextBox(QWidget *parent) : QTextBrowser(parent)
 //		wxT("<BODY><FONT COLOR = #%%s>%%s</FONT></BODY></HTML>"),
 //		wxFontMapper::GetEncodingName(wxLocale::GetSystemEncoding()).wx_str()
 //	);
-//	wxString fontName(m_font.GetFaceName());
-//	SetStandardFonts(m_font.GetPointSize(), fontName, fontName);
 }
 
 QspTextBox::~QspTextBox()
@@ -40,7 +41,7 @@ void QspTextBox::SetIsHtml(bool isHtml)
 
 void QspTextBox::RefreshUI(bool isScroll)
 {
-    QString color(QSPTools::GetHexColor(GetForegroundColour()));
+    QString color(QSPTools::GetHexColor(GetForegroundColor()));
     QString str(QByteArray::fromPercentEncoding(m_text.toUtf8()));
     QString text(QSPTools::HtmlizeWhitespaces(m_isUseHtml ? str : QSPTools::ProceedAsPlain(str)));
     //TODO: set colour and font
@@ -87,26 +88,28 @@ void QspTextBox::SetText(const QString& text, bool isScroll)
     }
 }
 
-void QspTextBox::SetTextFont(const QFont& font)
+void QspTextBox::SetTextFont(const QFont& new_font)
 {
-    if (m_font != font)
+    if (m_font != new_font)
     {
-        m_font = font;
-        setFont(font); //TODO: check which one of this to use
-        setCurrentFont(font);
+        m_font = new_font;
+        setFont(new_font); //TODO: check which one of this to use
+        setCurrentFont(new_font);
     }
 }
 
-bool QspTextBox::SetLinkColor(const QColor &colour)
+bool QspTextBox::SetLinkColor(const QColor &color)
 {
     //QPalette p = palette();
     //p.setBrush( QPalette::Link, clr);
     //setPalette( p );
-    if(m_linkColor != colour)
+    //NOTE: From Qt documentation:
+    //Note that we do not use the Link and LinkVisited roles when rendering rich text in Qt, and that we recommend that you use CSS and the QTextDocument::setDefaultStyleSheet() function to alter the appearance of links.
+    if(m_linkColor != color)
     {
-        QString sheet = QString::fromLatin1("a { text-decoration: underline; color: %1 }").arg(colour.name());
+        QString sheet = QString::fromLatin1("a { text-decoration: underline; color: %1 }").arg(color.name());
         document()->setDefaultStyleSheet(sheet);
-        m_linkColor = colour;
+        m_linkColor = color;
         RefreshUI();
         return true;
     }
@@ -126,38 +129,47 @@ void QspTextBox::SetBackgroundImage(const QPixmap& bmpBg)
     //CalcImageSize();
 }
 
-//Returns the background colour of the window.
-QColor QspTextBox::GetBackgroundColour()
+//Returns the background color of the window.
+QColor QspTextBox::GetBackgroundColor()
 {
-    return textBackgroundColor();
+    return m_backColor;
 }
 
 //The meaning of foreground colour varies according to the window class; it may be the text colour or other colour, or it may not be used at all. Additionally, not all native controls support changing their foreground colour so this method may change their colour only partially or even not at all.
-QColor QspTextBox::GetForegroundColour()
+QColor QspTextBox::GetForegroundColor()
 {
-    return textColor();
+    return m_fontColor;
 }
 
-//Returns true if the colour was really changed, false if it was already set to this colour and nothing was done.
-bool QspTextBox::SetBackgroundColour(const QColor &colour)
+//Returns true if the color was really changed, false if it was already set to this color and nothing was done.
+bool QspTextBox::SetBackgroundColor(const QColor &color)
 {
-    if(textBackgroundColor() != colour)
+    if(m_backColor != color)
     {
         //QPalette p = palette();
-        //p.setColor(QPalette::Base, colour);
+        //p.setColor(QPalette::Base, color);
         //setPalette(p);
-        setTextBackgroundColor(colour);
+        m_backColor = color;
+        setTextBackgroundColor(color);
         RefreshUI();
         return true;
     }
     return false;
 }
 
-bool QspTextBox::SetForegroundColour(const QColor &colour)
+bool QspTextBox::SetForegroundColor(const QColor &color)
 {
-    if(textColor() != colour)
+    //TODO: find alternative
+    //NOTE: From Qt documentation
+    //Warning: Do not use this function (void 	setPalette(const QPalette &)) in conjunction with Qt Style Sheets.
+    if(m_fontColor != color)
     {
-        setTextColor(colour);
+        m_fontColor = color;
+        setTextColor(color);
+        QPalette p = palette();
+        p.setColor(QPalette::Text, color);
+        p.setColor(QPalette::WindowText, color);
+        setPalette(p);
         RefreshUI();
         return true;
     }

+ 8 - 6
qsptextbox.h

@@ -27,17 +27,17 @@ public:
     // Accessors
     void SetIsHtml(bool isHtml);
     void SetText(const QString& text, bool isScroll = false);
-    void SetTextFont(const QFont& font);
+    void SetTextFont(const QFont& new_font);
     QFont GetTextFont() const { return m_font; }
     QString GetText() const { return m_text; }
-    bool SetLinkColor(const QColor &colour);
+    bool SetLinkColor(const QColor &color);
     QColor GetLinkColor() { return m_linkColor; }
     void SetGamePath(const QString& path);
     void SetBackgroundImage(const QPixmap& bmpBg);
-    QColor GetBackgroundColour();
-    QColor GetForegroundColour(); //text color
-    bool SetBackgroundColour(const QColor& colour);
-    bool SetForegroundColour(const QColor& colour);
+    QColor GetBackgroundColor();
+    QColor GetForegroundColor(); //text color
+    bool SetBackgroundColor(const QColor& color);
+    bool SetForegroundColor(const QColor& color);
     void SetShowPlainText(bool isPlain);
 
 private:
@@ -56,6 +56,8 @@ private:
     int m_posX;
     int m_posY;
     QColor m_linkColor;
+    QColor m_backColor;
+    QColor m_fontColor;
     bool showPlainText;
 };