OptionsToolBar.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. /**
  20. * The options toolbar is created and initialized here.
  21. */
  22. function OptionsToolBar() {
  23. }
  24. OptionsToolBar.includeBasePath = includeBasePath;
  25. OptionsToolBar.normalizeSeparators = function(action) {
  26. // remove double separators:
  27. var optionsToolBar = EAction.getOptionsToolBar();
  28. var children = optionsToolBar.children();
  29. var previousWidgetWasSeparator = false;
  30. for (var i = 0; i < children.length; ++i) {
  31. var c = children[i];
  32. if (isFunction(c.isSeparator) && c.isSeparator()===true) {
  33. if (previousWidgetWasSeparator) {
  34. var idx = action.optionWidgetActions.indexOf(c);
  35. if (idx!==-1) {
  36. // make sure the action is also removed from the list of added actions:
  37. action.optionWidgetActions.splice(idx, 1);
  38. }
  39. destr(c);
  40. }
  41. previousWidgetWasSeparator = true;
  42. }
  43. else if (isQWidget(c) && c.objectName.length!==0 && c.visible) {
  44. previousWidgetWasSeparator = false;
  45. }
  46. }
  47. };
  48. OptionsToolBar.getFirstInputWidget = function() {
  49. var optionsToolBar = EAction.getOptionsToolBar();
  50. var widgets = getWidgets(optionsToolBar);
  51. for (var p in widgets) {
  52. var w = widgets[p];
  53. if (isOfType(w, QLineEdit) ||
  54. isOfType(w, RMathLineEdit) ||
  55. isOfType(w, QSpinBox) ||
  56. isOfType(w, QComboBox)) {
  57. return w;
  58. }
  59. }
  60. return undefined;
  61. };
  62. OptionsToolBar.init = function(basePath) {
  63. // make sure that the options tool bar is initialized before
  64. // positions are restored from the config file:
  65. var optionsToolBar = EAction.getOptionsToolBar();
  66. //optionsToolBar.minimumWidth = 600;
  67. };
  68. OptionsToolBar.initStyle = function() {
  69. var optionsToolBar = EAction.getOptionsToolBar();
  70. var iconLabel = optionsToolBar.findChild("Icon");
  71. // style of label with current tool icon:
  72. if (!RSettings.hasCustomStyleSheet()) {
  73. var w = optionsToolBar.iconSize.width() < 20 ? 1 : 2;
  74. if (RSettings.hasDarkGuiBackground()) {
  75. iconLabel.styleSheet =
  76. "QLabel {"
  77. + "border-radius: %1px; ".arg(w*3)
  78. + "background-color: "
  79. + " qlineargradient(spread:pad, "
  80. + " x1: 0, y1: 0, "
  81. + " x2: 0, y2: 1, "
  82. + " stop: 0 #555555, "
  83. + " stop: 0.5 #606060, "
  84. + " stop: 1 #555555"
  85. + " ); "
  86. + "border: %1px solid #a0a0a0;".arg(w)
  87. + "margin: %1px %1px %1px %1px;".arg(w)
  88. + "}";
  89. }
  90. else {
  91. iconLabel.styleSheet =
  92. "QLabel {"
  93. + "border-radius: %1px; ".arg(w*3)
  94. + "background-color: "
  95. + " qlineargradient(spread:pad, "
  96. + " x1: 0, y1: 0, "
  97. + " x2: 0, y2: 1, "
  98. + " stop: 0 rgba(255,255,255,0), "
  99. + " stop: 0.5 rgba(255,255,255,192), "
  100. + " stop: 1 rgba(255,255,255,0) "
  101. + " ); "
  102. + "border: %1px solid #8f8f8f;".arg(w)
  103. + "margin: %1px %1px %1px %1px;".arg(w)
  104. + "}";
  105. }
  106. }
  107. };
  108. OptionsToolBar.postInit = function(basePath) {
  109. var optionsToolBar = EAction.getOptionsToolBar();
  110. // fixed height to prevent FS#652 (can happen at least on Windows and Linux):
  111. var ftb = EAction.getMainWindow().findChild("FileToolBar");
  112. if (!isNull(ftb)) {
  113. optionsToolBar.setFixedHeight(Math.min(ftb.sizeHint.height(), ftb.sizeHint.width()));
  114. }
  115. var flags = makeQtToolBarAreas(Qt.TopToolBarArea, Qt.BottomToolBarArea);
  116. optionsToolBar.setAllowedAreas(flags);
  117. // floatable tool bar does not resize properly: prevent floating of tool bar here:
  118. //optionsToolBar.floatable = false;
  119. var iconLabel = new QLabel(optionsToolBar);
  120. //var iconLabel = new QToolButton(optionsToolBar);
  121. iconLabel.objectName = "Icon";
  122. iconLabel.alignment = Qt.AlignCenter;
  123. iconLabel.setContentsMargins(6, 0, 6, 0);
  124. //iconLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken);
  125. OptionsToolBar.initStyle();
  126. // avoid empty label after startup, before initializing new document:
  127. OptionsToolBar.setIcon("scripts/Reset/Reset.svg");
  128. optionsToolBar.addWidget(iconLabel);
  129. optionsToolBar.addSeparator();
  130. var appWin = RMainWindowQt.getMainWindow();
  131. var pl = new RPaletteListenerAdapter();
  132. appWin.addPaletteListener(pl);
  133. pl.paletteChanged.connect(OptionsToolBar.initStyle);
  134. };
  135. /**
  136. * Sets the icon at the left of the options toolbar, typically indicating the current tool.
  137. */
  138. OptionsToolBar.setIcon = function(fileNameOrIcon) {
  139. var optionsToolBar = EAction.getOptionsToolBar();
  140. var iconLabel = optionsToolBar.findChild("Icon");
  141. var f = 1.0;
  142. if (RS.getSystemId()==="win") {
  143. f = 0.8;
  144. }
  145. else if (RS.getSystemId()==="osx") {
  146. f = 0.9;
  147. }
  148. else if (RS.getSystemId()==="linux") {
  149. f = 0.8;
  150. }
  151. var w = optionsToolBar.iconSize.width()*f;
  152. var h = optionsToolBar.iconSize.height()*f;
  153. // var iconSize = RSettings.getIntValue("CadToolBar/IconSize", 32);
  154. // iconSize = Math.max(iconSize, RSettings.getIntValue("ToolBar/IconSize", 32));
  155. // iconSize = Math.max(iconSize, RSettings.getIntValue("CadToolMatrix/IconSize", 24));
  156. // // retina icons:
  157. // if (RSettings.getDevicePixelRatio()>1) {
  158. // iconSize*=RSettings.getDevicePixelRatio();
  159. // }
  160. // var pm = new QPixmap(iconSize,iconSize);
  161. // //pm.fill(Qt.transparent);
  162. // var p = new QPainter();
  163. // p.begin(pm);
  164. // var renderer = new QSvgRenderer(OptionsToolBar.includeBasePath + "/IconBackground.svg");
  165. // renderer.render(p, new QRectF(0, 0, iconSize, iconSize));
  166. if (isOfType(fileNameOrIcon, QIcon)) {
  167. iconLabel.pixmap = fileNameOrIcon.pixmap(new QSize(w, h));
  168. //iconLabel.icon = fileNameOrIcon;
  169. }
  170. else {
  171. iconLabel.pixmap = new QIcon(autoPath(fileNameOrIcon)).pixmap(new QSize(w, h));
  172. //iconLabel.icon = new QIcon(autoPath(fileNameOrIcon));
  173. }
  174. // p.end();
  175. };