AbaKus – a complex calculator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7KB

  1. /*
  2. * dragsupport.cpp - part of abakus
  3. * Copyright (C) 2004, 2005 Michael Pyne <michael.pyne@kdemail.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <tqstring.h>
  20. #include <tqpixmap.h>
  21. #include <tqimage.h>
  22. #include <tqpainter.h>
  23. #include <tqcolor.h>
  24. #include <tqfont.h>
  25. #include <tqbrush.h>
  26. #include <tqfontmetrics.h>
  27. #include "dragsupport.h"
  28. namespace DragSupport
  29. {
  30. TQPixmap makePixmap(const TQString &text, const TQFont &font)
  31. {
  32. TQColor background(234, 178, 230);
  33. TQFontMetrics fm(font);
  34. int height = 2 * fm.height();
  35. TQSize bonusSize (height, 0);
  36. TQSize size(fm.width(text), height);
  37. TQImage image(size + bonusSize, 32);
  38. image.setAlphaBuffer(false);
  39. image.fill(0); // All transparent pixels
  40. image.setAlphaBuffer(true);
  41. TQPixmap pix(size + bonusSize);
  42. pix.fill(TQt::magenta); // Watch for incoming hacks
  43. TQPainter painter(&pix);
  44. painter.setFont(font);
  45. // Outline black, background white
  46. painter.setPen(TQt::black);
  47. painter.setBrush(background);
  48. // roundRect is annoying in that the four "pies" in each corner aren't
  49. // circular, they're elliptical. Try to make the radii force it circular
  50. // again.
  51. painter.drawRoundRect(pix.rect(), 75 * pix.height() / pix.width(), 75);
  52. // Alias better names for some constants.
  53. int textLeft = height / 2;
  54. // Draw text
  55. painter.setPen(TQt::black);
  56. painter.drawText(textLeft, height / 4, size.width(), size.height(), 0, text);
  57. TQImage overlay(pix.convertToImage());
  58. // The images should have the same size, copy pixels from overlay to the
  59. // bottom unless the pixel is called magenta. The pixels we don't copy
  60. // are transparent in the TQImage, and will remain transparent when
  61. // converted to a TQPixmap.
  62. for(int i = 0; i < image.width(); ++i)
  63. for(int j = 0; j < image.height(); ++j) {
  64. if(TQColor(overlay.pixel(i, j)) != TQt::magenta)
  65. image.setPixel(i, j, overlay.pixel(i, j));
  66. }
  67. pix.convertFromImage(image);
  68. return pix;
  69. }
  70. } // DragSupport