Skip to content

Commit

Permalink
Adjust colors if dark color scheme is used
Browse files Browse the repository at this point in the history
  • Loading branch information
bauerj committed Oct 4, 2017
1 parent 91ed74a commit 5c2beb4
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 26 deletions.
1 change: 1 addition & 0 deletions gui/qt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def __init__(self, config, daemon, plugins):
self.tray.show()
self.app.new_window_signal.connect(self.start_new_window)
run_hook('init_qt', self)
ColorScheme.update_from_widget(QWidget())

def build_tray_menu(self):
# Avoid immediate GC of old menu when window closed via its action
Expand Down
3 changes: 2 additions & 1 deletion gui/qt/address_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def __init__(self, parent, address):
vbox.addWidget(QLabel(_("Address:")))
self.addr_e = ButtonsLineEdit(self.address)
self.addr_e.addCopyButton(self.app)
self.addr_e.addButton(":icons/qrcode.png", self.show_qr, _("Show QR Code"))
icon = ":icons/qrcode_white.png" if ColorScheme.dark_scheme else ":icons/qrcode.png"
self.addr_e.addButton(icon, self.show_qr, _("Show QR Code"))
self.addr_e.setReadOnly(True)
vbox.addWidget(self.addr_e)

Expand Down
4 changes: 2 additions & 2 deletions gui/qt/address_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def on_update(self):
address_item.setData(0, Qt.UserRole, address)
address_item.setData(0, Qt.UserRole+1, True) # label can be edited
if self.wallet.is_frozen(address):
address_item.setBackground(0, QColor('lightblue'))
address_item.setBackground(0, ColorScheme.BLUE.as_color(True))
if self.wallet.is_beyond_limit(address, is_change):
address_item.setBackground(0, QColor('red'))
address_item.setBackground(0, ColorScheme.RED.as_color(True))
if is_used:
if not used_flag:
seq_item.insertChild(0, used_item)
Expand Down
26 changes: 13 additions & 13 deletions gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def connect_fields(self, window, btc_e, fiat_e, fee_e):
def edit_changed(edit):
if edit.follows:
return
edit.setStyleSheet(BLACK_FG)
edit.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
fiat_e.is_last_edited = (edit == fiat_e)
amount = edit.get_amount()
rate = self.fx.exchange_rate() if self.fx else None
Expand All @@ -655,15 +655,15 @@ def edit_changed(edit):
if edit is fiat_e:
btc_e.follows = True
btc_e.setAmount(int(amount / Decimal(rate) * COIN))
btc_e.setStyleSheet(BLUE_FG)
btc_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet())
btc_e.follows = False
if fee_e:
window.update_fee()
else:
fiat_e.follows = True
fiat_e.setText(self.fx.ccy_amount_str(
amount * Decimal(rate) / COIN, False))
fiat_e.setStyleSheet(BLUE_FG)
fiat_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet())
fiat_e.follows = False

btc_e.follows = False
Expand Down Expand Up @@ -1111,22 +1111,22 @@ def reset_max(t):
def entry_changed():
text = ""
if self.not_enough_funds:
amt_color, fee_color = RED_FG, RED_FG
amt_color, fee_color = ColorScheme.RED, ColorScheme.RED
text = _( "Not enough funds" )
c, u, x = self.wallet.get_frozen_balance()
if c+u+x:
text += ' (' + self.format_amount(c+u+x).strip() + ' ' + self.base_unit() + ' ' +_("are frozen") + ')'

elif self.fee_e.isModified():
amt_color, fee_color = BLACK_FG, BLACK_FG
amt_color, fee_color = ColorScheme.DEFAULT, ColorScheme.DEFAULT
elif self.amount_e.isModified():
amt_color, fee_color = BLACK_FG, BLUE_FG
amt_color, fee_color = ColorScheme.DEFAULT, ColorScheme.BLUE
else:
amt_color, fee_color = BLUE_FG, BLUE_FG
amt_color, fee_color = ColorScheme.BLUE, ColorScheme.BLUE

self.statusBar().showMessage(text)
self.amount_e.setStyleSheet(amt_color)
self.fee_e.setStyleSheet(fee_color)
self.amount_e.setStyleSheet(amt_color.as_stylesheet())
self.fee_e.setStyleSheet(fee_color.as_stylesheet())

self.amount_e.textChanged.connect(entry_changed)
self.fee_e.textChanged.connect(entry_changed)
Expand Down Expand Up @@ -2311,7 +2311,7 @@ def get_pk():
return keystore.get_private_keys(text)

f = lambda: button.setEnabled(get_address() is not None and get_pk() is not None)
on_address = lambda text: address_e.setStyleSheet(BLACK_FG if get_address() else RED_FG)
on_address = lambda text: address_e.setStyleSheet((ColorScheme.DEFAULT if get_address() else ColorScheme.RED).as_stylesheet())
keys_e.textChanged.connect(f)
address_e.textChanged.connect(f)
address_e.textChanged.connect(on_address)
Expand Down Expand Up @@ -2474,9 +2474,9 @@ def set_alias_color():
return
if self.alias_info:
alias_addr, alias_name, validated = self.alias_info
alias_e.setStyleSheet(GREEN_BG if validated else RED_BG)
alias_e.setStyleSheet((ColorScheme.GREEN if validated else ColorScheme.RED).as_stylesheet(True))
else:
alias_e.setStyleSheet(RED_BG)
alias_e.setStyleSheet(ColorScheme.RED.as_stylesheet(True))
def on_alias_edit():
alias_e.setStyleSheet("")
alias = str(alias_e.text())
Expand Down Expand Up @@ -2505,7 +2505,7 @@ def on_alias_edit():
SSL_error = None
SSL_id_label = HelpLabel(_('SSL certificate') + ':', msg)
SSL_id_e = QLineEdit(SSL_identity)
SSL_id_e.setStyleSheet(RED_BG if SSL_error else GREEN_BG if SSL_identity else '')
SSL_id_e.setStyleSheet((ColorScheme.RED if SSL_error else ColorScheme.GREEN).as_stylesheet(True) if SSL_identity else '')
if SSL_error:
SSL_id_e.setToolTip(SSL_error)
SSL_id_e.setReadOnly(True)
Expand Down
4 changes: 2 additions & 2 deletions gui/qt/paytoedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def setFrozen(self, b):
button.setHidden(b)

def setGreen(self):
self.setStyleSheet(util.GREEN_BG)
self.setStyleSheet(util.ColorScheme.GREEN.as_stylesheet(True))

def setExpired(self):
self.setStyleSheet(util.RED_BG)
self.setStyleSheet(util.ColorScheme.RED.as_stylesheet(True))

def parse_address_and_amount(self, line):
x, y = line.split(',')
Expand Down
5 changes: 3 additions & 2 deletions gui/qt/qrtextedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QFileDialog

from .util import ButtonsTextEdit, MessageBoxMixin
from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme


class ShowQRTextEdit(ButtonsTextEdit):
Expand Down Expand Up @@ -42,7 +42,8 @@ def __init__(self, text=""):
ButtonsTextEdit.__init__(self, text)
self.setReadOnly(0)
self.addButton(":icons/file.png", self.file_input, _("Read file"))
self.addButton(":icons/qrcode.png", self.qr_input, _("Read QR code"))
icon = ":icons/qrcode_white.png" if ColorScheme.dark_scheme else ":icons/qrcode.png"
self.addButton(icon, self.qr_input, _("Read QR code"))
run_hook('scan_text_edit', self)

def file_input(self):
Expand Down
40 changes: 35 additions & 5 deletions gui/qt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
else:
MONOSPACE_FONT = 'monospace'

GREEN_BG = "QWidget {background-color:#80ff80;}"
RED_BG = "QWidget {background-color:#ffcccc;}"
RED_FG = "QWidget {color:red;}"
BLUE_FG = "QWidget {color:blue;}"
BLACK_FG = "QWidget {color:black;}"

dialogs = []

Expand Down Expand Up @@ -602,6 +597,41 @@ def stop(self):
self.tasks.put(None)


class ColorSchemeItem:
def __init__(self, fg_color, bg_color):
self.colors = (fg_color, bg_color)

def _get_color(self, background):
return self.colors[(int(background) + int(ColorScheme.dark_scheme)) % 2]

def as_stylesheet(self, background=False):
css_prefix = "background-" if background else ""
color = self._get_color(background)
return "QWidget {{ {}color:{}; }}".format(css_prefix, color)

def as_color(self, background=False):
color = self._get_color(background)
return QColor(color)


class ColorScheme:
dark_scheme = False

GREEN = ColorSchemeItem("#117c11", "#8af296")
RED = ColorSchemeItem("#7c1111", "#f18c8c")
BLUE = ColorSchemeItem("#123b7c", "#8cb3f2")
DEFAULT = ColorSchemeItem("black", "white")

@staticmethod
def has_dark_background(widget):
brightness = sum(widget.palette().color(QPalette.Background).getRgb()[0:3])
return brightness < (255*3/2)

@staticmethod
def update_from_widget(widget):
if ColorScheme.has_dark_background(widget):
ColorScheme.dark_scheme = True

if __name__ == "__main__":
app = QApplication([])
t = WaitingDialog(None, 'testing ...', lambda: [time.sleep(1)], lambda x: QMessageBox.information(None, 'done', "done"))
Expand Down
2 changes: 1 addition & 1 deletion gui/qt/utxo_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def on_update(self):
utxo_item.setFont(4, QFont(MONOSPACE_FONT))
utxo_item.setData(0, Qt.UserRole, name)
if self.wallet.is_frozen(address):
utxo_item.setBackground(0, QColor('lightblue'))
utxo_item.setBackground(0, ColorScheme.BLUE.as_color(True))
self.addChild(utxo_item)

def create_menu(self, position):
Expand Down
1 change: 1 addition & 0 deletions icons.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<file>icons/microphone.png</file>
<file>icons/network.png</file>
<file>icons/qrcode.png</file>
<file>icons/qrcode_white.png</file>
<file>icons/preferences.png</file>
<file>icons/seed.png</file>
<file>icons/status_connected.png</file>
Expand Down
Binary file added icons/qrcode_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c2beb4

Please sign in to comment.