/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2015 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

var selectedFg          = "#0177FF",
    selectedBg          = "#FFF",
    uncheckedFg         = "#99bbff",
    bigBoxBorderColor   = "#AAA",
    smallBoxBorderColor = "#E8E8E8",
    smallBoxBg          = "#E8E8E8",
    generalFontSize     = "10pt",
    borderRadius        = "4px",
    groupBoxPadding     = "1ex",
    groupBoxTitleFontSize=generalFontSize,
    tableHeadingFontSize= "10pt",
    multiButtonMargin   = "0.5ex",
    windowBg            = "#FAAFAAFAA",
    toolbarBg           = "#DFFDFFDFF",
    layeredWindowBg     = windowBg,
    ;

switch (mainwindow.getWindowSystem()) {
  case mainwindow.MAC: generalFontSize       = "12pt";
                       groupBoxTitleFontSize = "10pt";
                       layerdWindowBg        = "transparent";
                       break;
  case mainwindow.X11: tableHeadingFontSize = "9pt";
                       break;
  case mainwindow.WIN: groupBoxPadding = "3ex;";
                       break;
}

var imageStyle = [ "* {",
    "border: none;",
    "font: 14px;",
"}"
].join(" ");

var generalStyle = [ "* {",
    "font-family: Open Sans, Helvetica, Verdana, sans-serif;", 
    "font-size: " + generalFontSize + ";",
    "font-weight: regular;",
    "color: rgb(68, 68, 68);",
    "background-color: " + selectedBg + ";",
    "selection-color: " + selectedFg + ";",
    "selection-background-color: " + selectedBg + ";",
"}",
"*:focus {",
  "border-color: " + selectedFg + ";",
"}",
"*:disabled {",
  "color: " + bigBoxBorderColor + ";",
"}",
"QScrollBar:horizontal {",
 "border: 1px solid " + smallBoxBorderColor + ";",
 "border-radius: " + borderRadius + ";",
 "background: " + smallBoxBg + ";",
 "height: 10px;",
 "margin: 0px 0px 0px 0px;",
"}",
"QScrollBar:vertical {",
  "border: 1px solid " + smallBoxBorderColor + ";",
  "border-radius: " + borderRadius + ";",
  "background: " + smallBoxBg + ";",
  "width: 10px;",
  "margin: 0px 0 0px 0;",
 "}",
 "QScrollBar::add-line:vertical {",
     "background: " + bigBoxBorderColor + ";",
     "height: 0px;",
     "subcontrol-position: bottom;",
     "subcontrol-origin: margin;",
 "}",
 "QScrollBar::add-line:horizontal {",
     "background: " + bigBoxBorderColor + ";",
     "width: 0px;",
     "subcontrol-position: bottom;",
     "subcontrol-origin: margin;",
 "}",
 "QScrollBar::sub-line:vertical {",
     "background: " + bigBoxBorderColor + ";",
     "height: 0px;",
     "subcontrol-position: top;",
     "subcontrol-origin: margin;",
 "}",
 "QScrollBar::sub-line:horizontal {",
     "background: " + bigBoxBorderColor + ";",
     "width: 0px;",
     "subcontrol-position: top;",
     "subcontrol-origin: margin;",
 "}",
"QScrollBar::down-arrow:vertical {",
     "width: 20px;",
     "height: 20px;",
"}",
"QScrollBar::up-arrow:vertical {",
     "width: 10px;",
     "height: 20px;",
"}", 
"QScrollBar::down-arrow:horizontal {",
     "width: 20px;",
     "height: 20px;",
     "background: " + selectedBg + ";",
"}",
"QScrollBar::up-arrow:horizontal {",
     "width: 10px;",
     "height: 20px;",
     "background: " + selectedBg + ";",
"}",
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {",
     "background: none;",
 "}",
"QScrollBar::handle {",
  "background-color: " + bigBoxBorderColor + ";",
  "border: 1px solid " + bigBoxBorderColor + ";",
  "border-radius: " + borderRadius + ";",
"}",
"QStatusBar {",
 //"background-color: rgb(1, 119, 255);",
"}",
"#desktopCollaborateLit, #desktopAccountingLit, #desktopManufactureLit, #_notice, #desktopPurchaseLit, #desktopSalesLit, #desktopSociallit {",
 "text-transform: lowercase;",
 "font-size: 18px;",
 "color: " + selectedFg + ";",
 "font-weight: 200;",
"}",
"QMenu {",
  "border: 1px solid " + bigBoxBorderColor + ";",
"}",
"QCheckBox {",
  "margin: " + multiButtonMargin + ";",
  "background-color: transparent;",
"}",
/* this checkbox styling is really ugly but will do if we cannot find a better solution
"QCheckBox::indicator:checked:focus {",
  "background-color: " + selectedFg + ";",
"}",
"QCheckBox::indicator:unchecked:focus {",
  "background-color: " + uncheckedFg + ";",
"}",
*/
"QGroupBox {",
  "text-transform: uppercase;",
  "font-size: " + groupBoxTitleFontSize + ";",
  "border-top: 1px solid " + smallBoxBorderColor + ";",
  "border-left: 1px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #e8e8e8, stop: 0.05 #e8e8e8, stop: 0.06 #FFFFFF, stop: 1 #FFFFFF);",
  "border-right: 1px solid qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #e8e8e8, stop: 0.05 #e8e8e8, stop: 0.06 #FFFFFF, stop: 1 #FFFFFF);",
  "margin-top: 1ex;",
  "padding: " + groupBoxPadding + ";",
"}",
"QGroupBox::title {",
 "color: rgb(138, 138, 138);",
 "subcontrol-origin: margin;",
 "subcontrol-position: top center;",
 "padding: 0 3px;",
"}",
"QTabBar::tab:selected {",
"background-color: " + selectedBg + ";",
"color: " + selectedFg + ";",
 "border-bottom-color: " + selectedBg + ";",
"}",
"QTabBar::tab:!selected {",
"background-color: " + selectedBg + ";",
"color: #666;",
"}",
 "QTabBar::tab:disabled {",
"background-color: " + selectedBg + ";",
"color: " +  bigBoxBorderColor + ";",
 "border-bottom-color: " + bigBoxBorderColor + ";",
"}",
"QTabBar::tab {",   
  "border: 1px solid " + bigBoxBorderColor + ";",
    "border-top-left-radius: " + borderRadius + ";",
    "border-top-right-radius: " + borderRadius + ";",
    "min-width: 8ex;",
    "padding-top: 2px;",
"padding-bottom: 2px;",
"padding-left: 4px;",
"padding-right: 4px;",
"}", 
"MenuButton::label {",
 "max-width: 90%;",
"}",
"VirtualClusterLineEdit QLabel {",
  "background-color: none;",
"}",
"QComboBox {",
  "padding-left:   4px;",
  "padding-right:  4px;",
  "padding-top:    2px;",
  "padding-bottom: 2px;",
  "margin: 2px;",
  "border-radius: " + borderRadius + ";",
  "border: 1px solid " + bigBoxBorderColor + ";",
"}",
"QComboBox:selected {",
  "border: 2px solid " + selectedFg + ";",
"}",
"QComboBox::editable:selected {",
  "border: 2px solid " + selectedFg + ";",
"}",
"QHeaderView::section {",
  "color: black;", // font-size doesn't work without this. why not?
  "font-size: "        + tableHeadingFontSize + ";",
  "background-color: " + smallBoxBg + ";",
"}",
"QLabel {",
  "background: transparent;",
"}",
"QMainWindow, XDialog, XWidget {",
  "background: " + windowBg + ";",
"}",
"QMainWindow QMainWindow, QMainWindow XWidget {",
  "background: " + layeredWindowBg + ";",
"}",
"QMenuBar::item:selected {",
  "background: " + selectedBg + ";",
  "selection-background-color:" + smallBoxBg + ";",
"}",
"QMenuBar::item:pressed {",
  "background: " + selectedBg + ";", 
  "color: "      + selectedFg + ";",
  "border-left:  1px solid " + bigBoxBorderColor + ";",
  "border-right: 1px solid " + bigBoxBorderColor + ";",
"}",
"QMenuBar::item:hover {",
  "color: " + selectedFg + ";",
"}",
"QPushButton {",
  "border-radius: " + borderRadius + ";",
  "border:  1px solid " + bigBoxBorderColor + ";",
  "margin:  0.75ex;",
  "padding: 0.50ex;",
"}",
"QPushButton:default {",
  "color:" + selectedFg + ";",
  "border:  1px solid " + bigBoxBorderColor + ";",
"}",
"QPushButton:hover {",
  "color: "            + selectedFg + ";",
  "background-color: " + selectedBg + ";",
  "border-color: "     + selectedFg + ";",
  "border-width: 2px;",
"}",
"QPushButton:pressed {",
  "color: "            + selectedBg + ";",
  "background-color: " + selectedFg + ";",
"}",
"QRadioButton {",
  "margin: " + multiButtonMargin + ";",
"}",
"QRadioButton::indicator {",
  "background-color: " + bigBoxBorderColor + ";",
  "width: 10px;",
  "height: 10px;",
"}",
"QRadioButton::indicator::unchecked {",
  "background-color: " + selectedBg + ";",
  "border: 2px solid " + bigBoxBorderColor + ";",
  "border-radius: 7px;",
"}",
"QRadioButton::indicator:unchecked:hover {",
  "background-color: " + selectedFg + ";",
  "border: 2px solid " + selectedBg + ";",  
  "border-radius: 7px;",
"}",
"QRadioButton::indicator:unchecked:pressed {",
  "background-color: " + selectedFg + ";",
  "border: 2px solid " + smallBoxBg + ";",
  "border-radius: 7px;",
"}",
"QRadioButton::indicator::checked {",
  "background-color: " + selectedFg + ";",
  "border: 2px solid " + selectedBg + ";",
  "border-radius: 7px;",
"}",
"QRadioButton::indicator:checked:hover {",
  "background-color: " + selectedFg + ";",
  "border: 2px solid " + selectedBg + ";",
  "border-radius: 7px;",
"}",
"QRadioButton::indicator:checked:pressed {",
  "background-color: " + selectedFg + ";",
  "border: 2px solid " + selectedBg + ";",
  "border-radius: 7px;",
"}",
"QTextEdit, QPlainTextEdit {",
 "font-family: courier new, courier, monospace;",
"}",
"QTextEdit:focus, QPlainTextEdit:focus {",
  "border: 2px solid " + selectedFg + ";",
"}",
"QToolBar {",
  "background: "       + toolbarBg + ";",
  "color: "            + selectedFg + ";",
  "border: 1px solid " + smallBoxBorderColor + ";",
"}",
"QToolBar::separator {",
  "color: "            + selectedFg + ";",
  "background-color: " + selectedBg + ";",
  "image: none;",
  "border: 1px solid " + bigBoxBorderColor + ";",
  "width: 0px;",
"}",
"QToolBar::handle {",
  "background-color: " + bigBoxBorderColor + ";",
  "border: 1px solid " + bigBoxBorderColor + ";",
  "image: none;",
  "width: 2px;",
  "height: 2px;",
"}",
"QToolButton {",
  "background-color: transparent;",
  "selection-color: " + selectedFg + ";",
"}",
"QToolButton:hover {",
  "border-top: none;",
  "border-left: none;",
  "border-right: none;",
  "border-bottom: 1px solid " + selectedFg + ";",
"}",
"QToolButton:on {",
  "border: 1px solid " + selectedFg + ";",
  "selection-color: " + selectedFg + ";",
  "color: " + selectedFg + ";",
"}",

"ParameterWidget QToolButton {",
  "border-radius: " + borderRadius + ";",
  "border:  1px solid " + bigBoxBorderColor + ";",
  "margin:  0.75ex;",
  "padding: 0.50ex;",
"}",
"ParameterWidget QToolButton:default {",
  "color:" + selectedFg + ";",
  "border:  1px solid " + smallBoxBorderColor + ";",
"}",
"ParameterWidget QToolButton:hover {",
  "border:  1px solid " + selectedFg + ";",
  "background-color: "  + selectedBg + ";",
"}",
"ParameterWidget QToolButton:pressed {",
  "color: "            + selectedBg + ";",
  "background-color: " + selectedFg + ";",
"}",

"QTreeView::item:selected QWidget {",
  "min-height: 16px;",
"}",
"QTreeView QScrollBar:horizontal {",
  "min-height: 10px;",
  "height: 10px;",
"}",

"QTreeView::item:selected QComboBox {",
  "color: " + selectedFg + ";",
  "font-size: " + generalFontSize + ";",
  "min-height: 15pt;", // generalFontSize + a bit
  "padding: 1px;",
  "margin: 0px;",
"}",

"QTreeWidget  {",
  "selection-background-color: transparent;",
"}"
].join(" ");

var desktopStyle = generalStyle +
[ "QTreeView {",
    "border-style: none;",
  "}",
  "QPushButton {",
    "border: none;",
  "}",
  "QWidget#dockWidgetContents QPushButton#_send {",
    "border-radius: " + borderRadius + ";",
    "border:  1px solid " + bigBoxBorderColor + ";",
    "margin:  0.75ex;",
    "padding: 0.50ex;",
  "}",
  "QHeaderView::section {",
    "background-color: transparent;",
  "}",
].join(" ");

try {
  if (! preferences.boolean("xtdesktop/useNativeStyle")) {
    QApplication.setStyleSheet(generalStyle);
  }
} catch(e) {
  QMessageBox.critical(mainwindow, "QApplication Error", e.message);
}
