mindmapwidgettoolbar.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "mindmapwidgettoolbar.h"
  2. #include "ui_mindmapwidgettoolbar.h"
  3. MindMapWidgetToolBar::MindMapWidgetToolBar(QWidget* parent)
  4. : QWidget(parent)
  5. , ui(new Ui::MindMapWidgetToolBar)
  6. {
  7. ui->setupUi(this);
  8. setMouseTracking(true);
  9. m_fontBoldRect.setRect(4, 4, 24, 24);
  10. m_fontItalicsRect.setRect(m_fontBoldRect.right() + 4, 4, 24, 24);
  11. m_underLineRect.setRect(m_fontItalicsRect.right() + 4, 4, 24, 24);
  12. m_strikeOutRect.setRect(m_underLineRect.right() + 4, 4, 24, 24);
  13. m_overLineRect.setRect(m_strikeOutRect.right() + 4, 4, 24, 24);
  14. m_fontBackColorRect.setRect(m_overLineRect.right() + 4, 4, 36, 24);
  15. m_fontBackColorLeftRect.setRect(m_overLineRect.right() + 4, 4, 24, 24);
  16. m_fontBackColorRightRect.setRect(m_fontBackColorLeftRect.right() + 1, 4, 12, 24);
  17. m_fontColorRect.setRect(m_fontBackColorRect.right() + 4, 4, 36, 24);
  18. m_fontColorLeftRect.setRect(m_fontBackColorRect.right() + 4, 4, 24, 24);
  19. m_fontColorRightRect.setRect(m_fontColorLeftRect.right() + 1, 4, 12, 24);
  20. m_strewRect.setRect(m_fontColorRect.right() + 4, 4, 24, 24);
  21. m_strewApplyRect.setRect(m_strewRect.right() + 4, 4, 24, 24);
  22. m_dropdownPix = QPixmap(":/res/toolbar/dropdown.png");
  23. m_pickerPix = QPixmap(":/res/toolbar/picker.png");
  24. }
  25. MindMapWidgetToolBar::~MindMapWidgetToolBar()
  26. {
  27. delete ui;
  28. }
  29. void MindMapWidgetToolBar::paintEvent(QPaintEvent* event)
  30. {
  31. QPoint pt = mapFromGlobal(cursor().pos());
  32. QFont font;
  33. QPen pen;
  34. QColor color;
  35. QPainter painter;
  36. int colorIndex;
  37. painter.begin(this);
  38. painter.setRenderHints(QPainter::HighQualityAntialiasing);
  39. painter.fillRect(rect(), QBrush(QColor(252, 252, 252)));
  40. painter.setPen(QColor(240, 240, 240));
  41. painter.drawLine(0, height(), width(), height());
  42. //绘制加粗按钮
  43. painter.save();
  44. painter.setBrush(getSelBrush(pt, m_fontBoldRect));
  45. painter.setPen(Qt::NoPen);
  46. painter.drawRect(m_fontBoldRect);
  47. painter.setPen(Qt::black);
  48. font = painter.font();
  49. font.setPointSize(17);
  50. font.setBold(true);
  51. painter.setFont(font);
  52. painter.drawText(m_fontBoldRect, Qt::AlignCenter, "B");
  53. painter.restore();
  54. //绘制文字倾斜按钮
  55. painter.save();
  56. painter.setBrush(getSelBrush(pt, m_fontItalicsRect));
  57. painter.setPen(Qt::NoPen);
  58. painter.drawRect(m_fontItalicsRect);
  59. painter.setPen(Qt::black);
  60. font = painter.font();
  61. font.setPointSize(17);
  62. font.setItalic(true);
  63. painter.setFont(font);
  64. painter.drawText(m_fontItalicsRect, Qt::AlignCenter, "I");
  65. painter.restore();
  66. //绘制下划线按钮
  67. painter.save();
  68. painter.setBrush(getSelBrush(pt, m_underLineRect));
  69. painter.setPen(Qt::NoPen);
  70. painter.drawRect(m_underLineRect);
  71. painter.setPen(Qt::black);
  72. font = painter.font();
  73. font.setPointSize(17);
  74. font.setUnderline(true);
  75. painter.setFont(font);
  76. painter.drawText(m_underLineRect, Qt::AlignCenter, "U");
  77. painter.restore();
  78. //删除线按钮
  79. painter.save();
  80. painter.setBrush(getSelBrush(pt, m_strikeOutRect));
  81. painter.setPen(Qt::NoPen);
  82. painter.drawRect(m_strikeOutRect);
  83. painter.setPen(Qt::black);
  84. font = painter.font();
  85. font.setPointSize(17);
  86. font.setStrikeOut(true);
  87. painter.setFont(font);
  88. painter.drawText(m_strikeOutRect, Qt::AlignCenter, "D");
  89. painter.restore();
  90. //删除线按钮
  91. painter.save();
  92. painter.setBrush(getSelBrush(pt, m_overLineRect));
  93. painter.setPen(Qt::NoPen);
  94. painter.drawRect(m_overLineRect);
  95. painter.setPen(Qt::black);
  96. font = painter.font();
  97. font.setPointSize(17);
  98. font.setOverline(true);
  99. painter.setFont(font);
  100. painter.drawText(m_overLineRect, Qt::AlignCenter, "A");
  101. painter.restore();
  102. //绘制文字背景按钮
  103. painter.save();
  104. painter.setBrush(Qt::NoBrush);
  105. painter.setPen(getSelPen(pt, m_fontBackColorRect));
  106. painter.drawRect(m_fontBackColorRect);
  107. painter.fillRect(m_fontBackColorLeftRect, getSelBrush(pt, m_fontBackColorLeftRect));
  108. painter.setPen(Qt::black);
  109. font = painter.font();
  110. font.setPointSize(17);
  111. painter.setFont(font);
  112. QRect fontBackRc = m_fontBackColorRect;
  113. fontBackRc.setWidth(24);
  114. painter.drawText(fontBackRc, Qt::AlignRight | Qt::AlignVCenter, "ab");
  115. pen = painter.pen();
  116. pen.setWidth(4);
  117. colorIndex = ColorTable::backColorIndex;
  118. if (colorIndex == ColorTable::lastIndex()) {
  119. pen.setStyle(Qt::NoPen);
  120. } else {
  121. pen.setColor(ColorTable::getColor(colorIndex));
  122. }
  123. painter.fillRect(m_fontBackColorRightRect, getSelBrush(pt, m_fontBackColorRightRect));
  124. painter.setPen(pen);
  125. painter.drawLine(fontBackRc.left() + 8, fontBackRc.bottom() - 2, fontBackRc.left() + 22, fontBackRc.bottom() - 2);
  126. QRect fontBackDownRc = m_fontBackColorRect;
  127. fontBackDownRc.setLeft(m_fontBackColorRect.right() - 10);
  128. fontBackDownRc.setTop(m_fontBackColorRect.top() + 4);
  129. fontBackDownRc.setWidth(8);
  130. fontBackDownRc.setHeight(16);
  131. painter.drawPixmap(fontBackDownRc, m_dropdownPix, m_dropdownPix.rect());
  132. painter.restore();
  133. //绘制文字颜色按钮
  134. painter.save();
  135. painter.setBrush(Qt::NoBrush);
  136. painter.setPen(getSelPen(pt, m_fontColorRect));
  137. painter.drawRect(m_fontColorRect);
  138. painter.fillRect(m_fontColorLeftRect, getSelBrush(pt, m_fontColorLeftRect));
  139. painter.setPen(Qt::black);
  140. font = painter.font();
  141. font.setPointSize(17);
  142. painter.setFont(font);
  143. QRect fontRc = m_fontColorRect;
  144. fontRc.setWidth(24);
  145. QRect textRc = fontRc;
  146. textRc.setLeft(textRc.left() + 4);
  147. painter.drawText(textRc, Qt::AlignCenter, "A");
  148. pen = painter.pen();
  149. pen.setWidth(4);
  150. colorIndex = ColorTable::fontColorIndex;
  151. if (colorIndex == 0) {
  152. pen.setColor(Qt::black);
  153. } else {
  154. pen.setColor(ColorTable::getColor(colorIndex));
  155. }
  156. painter.fillRect(m_fontColorRightRect, getSelBrush(pt, m_fontColorRightRect));
  157. painter.setPen(pen);
  158. painter.drawLine(fontRc.left() + 8, fontRc.bottom() - 2, fontRc.left() + 22, fontRc.bottom() - 2);
  159. QRect fontDownRc = m_fontColorRect;
  160. fontDownRc.setLeft(m_fontColorRect.right() - 10);
  161. fontDownRc.setTop(m_fontColorRect.top() + 4);
  162. fontDownRc.setWidth(8);
  163. fontDownRc.setHeight(16);
  164. painter.drawPixmap(fontDownRc, m_dropdownPix, m_dropdownPix.rect());
  165. painter.restore();
  166. //吸管工具
  167. painter.save();
  168. painter.setBrush(getSelBrush(pt, m_strewRect));
  169. painter.setPen(Qt::NoPen);
  170. painter.drawRect(m_strewRect);
  171. QRect textBkRc;
  172. textBkRc.setRect(m_strewRect.left() + 2, m_strewRect.top() + 2,
  173. m_strewRect.width() - 4, m_strewRect.height() - 4);
  174. painter.fillRect(textBkRc, getBkBrush());
  175. QRect pickerRc;
  176. pickerRc.setRect(m_strewRect.right() - 8, m_strewRect.bottom() - 8, 8, 8);
  177. painter.drawPixmap(pickerRc, m_pickerPix, m_pickerPix.rect());
  178. painter.setPen(ColorTable::getColor(ColorTable::fontColorIndex));
  179. font = painter.font();
  180. font.setPointSize(14);
  181. font.setBold(true);
  182. painter.setFont(font);
  183. painter.drawText(m_strewRect, Qt::AlignCenter, "Sr");
  184. painter.restore();
  185. //绘制画刷应用按钮
  186. painter.save();
  187. painter.setBrush(getSelBrush(pt, m_strewApplyRect));
  188. painter.setPen(Qt::NoPen);
  189. painter.drawRect(m_strewApplyRect);
  190. QRect textApplyBkRc;
  191. textApplyBkRc.setRect(m_strewApplyRect.left() + 2, m_strewApplyRect.top() + 2,
  192. m_strewApplyRect.width() - 4, m_strewApplyRect.height() - 4);
  193. painter.fillRect(textApplyBkRc, getBkBrush());
  194. painter.setPen(ColorTable::getColor(ColorTable::fontColorIndex));
  195. font = painter.font();
  196. font.setPointSize(17);
  197. font.setBold(true);
  198. painter.setFont(font);
  199. painter.drawText(m_strewApplyRect, Qt::AlignCenter, "Sr");
  200. painter.restore();
  201. painter.end();
  202. }
  203. void MindMapWidgetToolBar::mouseMoveEvent(QMouseEvent* event)
  204. {
  205. update();
  206. }
  207. void MindMapWidgetToolBar::setMindMapWidget(MindMapWidget* mindMapWidget)
  208. {
  209. m_mindMapWidget = mindMapWidget;
  210. }
  211. void MindMapWidgetToolBar::mousePressEvent(QMouseEvent* event)
  212. {
  213. QPoint pt = mapFromGlobal(cursor().pos());
  214. if (m_fontBoldRect.contains(pt)) {
  215. m_mindMapWidget->setNodeFontStyle(SET_BOLD);
  216. } else if (m_fontItalicsRect.contains(pt)) {
  217. m_mindMapWidget->setNodeFontStyle(SET_ITALICS);
  218. } else if (m_underLineRect.contains(pt)) {
  219. m_mindMapWidget->setNodeFontStyle(SET_UNDERLINE);
  220. } else if (m_overLineRect.contains(pt)) {
  221. m_mindMapWidget->setNodeFontStyle(SET_OVERLINE);
  222. } else if (m_strikeOutRect.contains(pt)) {
  223. m_mindMapWidget->setNodeFontStyle(SET_STRIKEOUT);
  224. } else if (m_fontBackColorLeftRect.contains(pt)) {
  225. m_mindMapWidget->setNodeFontStyle(SET_FONT_BACKCOLOR);
  226. } else if (m_fontBackColorRightRect.contains(pt)) {
  227. m_mindMapWidget->showBackColorEditDialog();
  228. } else if (m_fontColorLeftRect.contains(pt)) {
  229. m_mindMapWidget->setNodeFontStyle(SET_FONT_COLOR);
  230. } else if (m_fontColorRightRect.contains(pt)) {
  231. m_mindMapWidget->showFontColorEditDialog();
  232. } else if (m_strewRect.contains(pt)) {
  233. m_mindMapWidget->setFontStyle();
  234. } else if (m_strewApplyRect.contains(pt)) {
  235. m_mindMapWidget->applyFontStyle();
  236. }
  237. }
  238. QBrush MindMapWidgetToolBar::getSelBrush(QPoint& pt, QRect& rc)
  239. {
  240. if (rc.contains(pt)) {
  241. return QBrush(QColor(197, 197, 197));
  242. } else {
  243. return QBrush(Qt::NoBrush);
  244. }
  245. }
  246. QPen MindMapWidgetToolBar::getSelPen(QPoint& pt, QRect& rc)
  247. {
  248. if (rc.contains(pt)) {
  249. return QPen(QColor(197, 197, 197));
  250. } else {
  251. return QPen(Qt::NoPen);
  252. }
  253. }
  254. QBrush MindMapWidgetToolBar::getBkBrush()
  255. {
  256. if (ColorTable::backColorIndex == ColorTable::lastIndex()) {
  257. return QBrush(Qt::NoBrush);
  258. } else {
  259. return QBrush(ColorTable::getColor(ColorTable::backColorIndex));
  260. }
  261. }