qtDescriptivePhraseDelegate.cxx 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=========================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//=========================================================================
#include "smtk/extension/qt/qtDescriptivePhraseDelegate.h"

#include "smtk/extension/qt/qtDescriptivePhraseEditor.h"
#include "smtk/extension/qt/qtDescriptivePhraseModel.h"

15
#include <QAbstractProxyModel>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>

namespace smtk
{
namespace extension
{

qtDescriptivePhraseDelegate::qtDescriptivePhraseDelegate(QWidget* owner)
  : QStyledItemDelegate(owner)
  , m_swatchSize(16)
  , m_titleFontSize(14)
  , m_subtitleFontSize(10)
  , m_titleFontWeight(2)
  , m_subtitleFontWeight(1)
  , m_textVerticalPad(2)
  , m_drawSubtitle(true)
34
  , m_visibilityMode(false)
35
  , m_highlightOnHover(false)
36 37 38 39 40
{
}

int qtDescriptivePhraseDelegate::swatchSize() const
{
41
  return m_swatchSize;
42 43 44 45
}

void qtDescriptivePhraseDelegate::setSwatchSize(int sfs)
{
46
  m_swatchSize = sfs;
47 48 49 50
}

int qtDescriptivePhraseDelegate::titleFontSize() const
{
51
  return m_titleFontSize;
52 53 54 55
}

void qtDescriptivePhraseDelegate::setTitleFontSize(int tfs)
{
56
  m_titleFontSize = tfs;
57 58 59 60
}

int qtDescriptivePhraseDelegate::subtitleFontSize() const
{
61
  return m_subtitleFontSize;
62 63 64 65
}

void qtDescriptivePhraseDelegate::setSubtitleFontSize(int sfs)
{
66
  m_subtitleFontSize = sfs;
67 68 69 70
}

int qtDescriptivePhraseDelegate::titleFontWeight() const
{
71
  return m_titleFontWeight;
72 73 74 75
}

void qtDescriptivePhraseDelegate::setTitleFontWeight(int tfw)
{
76
  m_titleFontWeight = tfw;
77 78 79 80
}

int qtDescriptivePhraseDelegate::subtitleFontWeight() const
{
81
  return m_subtitleFontWeight;
82 83 84 85
}

void qtDescriptivePhraseDelegate::setSubtitleFontWeight(int sfw)
{
86
  m_subtitleFontWeight = sfw;
87 88 89 90
}

int qtDescriptivePhraseDelegate::textVerticalPad() const
{
91
  return m_textVerticalPad;
92 93 94 95
}

void qtDescriptivePhraseDelegate::setTextVerticalPad(int tvp)
{
96
  m_textVerticalPad = tvp;
97 98 99 100
}

bool qtDescriptivePhraseDelegate::drawSubtitle() const
{
101
  return m_drawSubtitle;
102 103 104 105
}

void qtDescriptivePhraseDelegate::setDrawSubtitle(bool includeSubtitle)
{
106
  m_drawSubtitle = includeSubtitle;
107 108
}

109 110
bool qtDescriptivePhraseDelegate::visibilityMode() const
{
111
  return m_visibilityMode;
112 113 114 115
}

void qtDescriptivePhraseDelegate::setVisibilityMode(bool allEditsChangeVisibility)
{
116
  m_visibilityMode = allEditsChangeVisibility;
117 118
}

119 120 121 122 123
bool qtDescriptivePhraseDelegate::highlightOnHover() const
{
  return m_highlightOnHover;
}

124 125 126 127 128
void qtDescriptivePhraseDelegate::setHighlightOnHover(bool highlightOnHover)
{
  m_highlightOnHover = highlightOnHover;
}

129 130 131 132 133 134
QSize qtDescriptivePhraseDelegate::sizeHint(
  const QStyleOptionViewItem& option, const QModelIndex& idx) const
{
  QIcon icon = qvariant_cast<QIcon>(idx.data(qtDescriptivePhraseModel::PhraseIconRole));
  QSize iconsize = icon.actualSize(option.decorationSize);
  QFont titleFont = QApplication::font();
135
  titleFont.setPixelSize(m_titleFontSize);
136 137 138
  titleFont.setBold(this->titleFontWeight() > 1 ? true : false);
  QFontMetrics titleFM(titleFont);
  QFont subtitleFont = QApplication::font();
139
  subtitleFont.setPixelSize(m_subtitleFontSize);
140 141
  subtitleFont.setBold(this->subtitleFontWeight() > 1 ? true : false);
  QFontMetrics subtitleFM(subtitleFont);
142 143
  int minHeight = titleFM.height() + 2 * m_textVerticalPad;
  if (m_drawSubtitle)
144 145 146 147 148 149 150 151
  {
    minHeight += subtitleFM.height();
  }
  if (minHeight < iconsize.height())
  {
    minHeight = iconsize.height();
  }

152
  return (QSize(iconsize.width() + m_swatchSize, minHeight));
153 154 155 156 157
}

void qtDescriptivePhraseDelegate::paint(
  QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& idx) const
{
158 159
  bool setBackground = false;
  QColor background;
160
  // If selected, draw the highlight color over entire rectangle
161 162 163 164 165 166 167 168 169 170 171
  if (option.state.testFlag(QStyle::State_MouseOver) && m_highlightOnHover)
  {
    background = option.palette.highlight().color().lighter(125);
    setBackground = true;
  }
  else if (option.state.testFlag(QStyle::State_Selected))
  {
    background = option.palette.highlight().color();
    setBackground = true;
  }
  if (setBackground)
172 173
  {
    painter->save();
174
    painter->setBrush(QBrush(background));
175 176 177 178
    painter->setPen(Qt::NoPen);
    painter->drawRect(option.rect);
    painter->restore();
  }
179 180 181 182 183 184

  painter->save();
  QIcon icon = qvariant_cast<QIcon>(idx.data(qtDescriptivePhraseModel::PhraseIconRole));
  QSize iconsize = icon.actualSize(option.decorationSize);
  QFont titleFont = QApplication::font();
  QFont subtitleFont = QApplication::font();
185
  titleFont.setPixelSize(m_titleFontSize);
186 187 188 189 190 191 192
  /// add a method to set/get title font
  titleFont.setBold(this->titleFontWeight() > 1 ? true : false);
  // bold the active model title
  if (idx.data(qtDescriptivePhraseModel::ModelActiveRole).toBool())
  {
    titleFont.setBold(true);
  }
193
  subtitleFont.setPixelSize(m_subtitleFontSize);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  subtitleFont.setBold(this->subtitleFontWeight() > 1 ? true : false);

  //subtitleFont.setWeight(subtitleFont.weight() - 2);
  QFontMetrics titleFM(titleFont);

  QString titleText = qvariant_cast<QString>(idx.data(qtDescriptivePhraseModel::TitleTextRole));
  if (idx.data(qtDescriptivePhraseModel::PhraseCleanRole).toInt() == 0)
  {
    titleText = titleText + " *";
  }
  QString subtitleText =
    qvariant_cast<QString>(idx.data(qtDescriptivePhraseModel::SubtitleTextRole));
  //std::cout << "Paint " << idx.internalPointer() << " " << idx.row() << " " << titleText.toStdString().c_str() << "\n";
  QColor swatchColor = qvariant_cast<QColor>(idx.data(qtDescriptivePhraseModel::PhraseColorRole));

  QRect titleRect = option.rect;
  QRect subtitleRect = option.rect;
  QRect iconRect = option.rect;
  QRect colorRect = option.rect;
  // visible icon
  QIcon visicon = qvariant_cast<QIcon>(idx.data(qtDescriptivePhraseModel::PhraseVisibilityRole));
  QSize visiconsize = visicon.actualSize(option.decorationSize);

  colorRect.setLeft(colorRect.left() + visiconsize.width() + 2);
218
  colorRect.setRight(colorRect.left() + m_swatchSize - 1);
219 220
  //  colorRect.setTop(colorRect.top() + 1);
  colorRect.setTop(colorRect.top() + 2);
221
  int swdelta = (colorRect.height() - m_swatchSize) / 2;
222 223 224 225 226 227 228
  swdelta = (swdelta < 0 ? 0 : swdelta);
  colorRect.adjust(0, swdelta, 0, -swdelta);
  iconRect.setLeft(colorRect.left());
  iconRect.setRight(iconRect.left() + iconsize.width() + 15);
  iconRect.setTop(iconRect.top() + 1);
  titleRect.setLeft(iconRect.right());
  subtitleRect.setLeft(iconRect.right());
229
  titleRect.setTop(titleRect.top() + m_textVerticalPad / 2.0);
230
  titleRect.setBottom(titleRect.top() +
231 232
    (m_drawSubtitle ? titleFM.height() : option.rect.height() - m_textVerticalPad));
  subtitleRect.setTop(titleRect.bottom() + m_textVerticalPad);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

  if (swatchColor.isValid())
  {
    painter->save();
    painter->setBrush(swatchColor);
    painter->setPen(Qt::NoPen);
    painter->drawRect(colorRect);
    painter->restore();
  }

  //painter->drawPixmap(QPoint(iconRect.right()/2,iconRect.top()/2),icon.pixmap(iconsize.width(),iconsize.height()));
  painter->drawPixmap(
    QPoint(iconRect.left(), iconRect.top() + (option.rect.height() - iconsize.height()) / 2.),
    icon.pixmap(iconsize.width(), iconsize.height()));

  if (!visicon.isNull())
    painter->drawPixmap(QPoint(option.rect.left(), colorRect.top()),
      visicon.pixmap(visiconsize.width(), visiconsize.height()));

252 253
  if (setBackground && background.lightness() < 128)
  {
254
    painter->setPen(Qt::white);
255
  }
256 257 258
  painter->setFont(titleFont);
  painter->drawText(titleRect, Qt::AlignVCenter, titleText);

259
  if (m_drawSubtitle)
260 261 262 263 264 265 266 267 268 269 270
  {
    painter->setFont(subtitleFont);
    painter->drawText(subtitleRect, subtitleText);
  }

  painter->restore();
}

QWidget* qtDescriptivePhraseDelegate::createEditor(
  QWidget* owner, const QStyleOptionViewItem& option, const QModelIndex& idx) const
{
271 272 273
  (void)option;

  // Visibility mode does not allow editing the title.
274 275 276
  if (m_visibilityMode)
  {
    return nullptr;
277 278 279 280 281 282 283 284 285
  }

  // Otherwise, edit the title if the item says we can.
  if (idx.data(qtDescriptivePhraseModel::TitleTextMutableRole).toBool())
  {
    smtk::extension::qtDescriptivePhraseEditor* editor = new qtDescriptivePhraseEditor(owner);
    return editor;
  }
  return nullptr;
286 287 288 289 290 291 292 293 294
}

void qtDescriptivePhraseDelegate::updateEditorGeometry(
  QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& idx) const
{
  QIcon icon = qvariant_cast<QIcon>(idx.data(qtDescriptivePhraseModel::PhraseIconRole));
  QSize iconsize = icon.actualSize(option.decorationSize);
  QFont titleFont = QApplication::font();
  QFont subtitleFont = QApplication::font();
295
  titleFont.setPixelSize(m_titleFontSize);
296 297 298 299 300 301 302
  /// add a method to set/get title font
  titleFont.setBold(this->titleFontWeight() > 1 ? true : false);
  // bold the active model title
  if (idx.data(qtDescriptivePhraseModel::ModelActiveRole).toBool())
  {
    titleFont.setBold(true);
  }
303
  subtitleFont.setPixelSize(m_subtitleFontSize);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  subtitleFont.setBold(this->subtitleFontWeight() > 1 ? true : false);

  //subtitleFont.setWeight(subtitleFont.weight() - 2);
  QFontMetrics titleFM(titleFont);

  QString titleText = qvariant_cast<QString>(idx.data(qtDescriptivePhraseModel::TitleTextRole));
  QString subtitleText =
    qvariant_cast<QString>(idx.data(qtDescriptivePhraseModel::SubtitleTextRole));
  if (idx.data(qtDescriptivePhraseModel::PhraseCleanRole).toInt() == 0)
  {
    titleText += " *";
  }

  QRect titleRect = option.rect;
  QRect iconRect = option.rect;
  QRect colorRect = option.rect;
  // visible icon
  QIcon visicon = qvariant_cast<QIcon>(idx.data(qtDescriptivePhraseModel::PhraseVisibilityRole));
  QSize visiconsize = visicon.actualSize(option.decorationSize);

  colorRect.setLeft(colorRect.left() + visiconsize.width() + 2);
325 326
  colorRect.setRight(colorRect.left() + m_swatchSize);
  int swdelta = (colorRect.height() - m_swatchSize) / 2;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  swdelta = (swdelta < 0 ? 0 : swdelta);
  colorRect.adjust(0, swdelta, 0, -swdelta);
  iconRect.setLeft(colorRect.left());
  iconRect.setRight(iconRect.left() + iconsize.width() + 15);
  iconRect.setTop(iconRect.top() + 1);
  titleRect.setLeft(iconRect.right());

  editor->setGeometry(titleRect);
}

void qtDescriptivePhraseDelegate::setEditorData(QWidget* editor, const QModelIndex& idx) const
{
  smtk::extension::qtDescriptivePhraseEditor* entityEditor =
    qobject_cast<smtk::extension::qtDescriptivePhraseEditor*>(editor);
  if (entityEditor)
  {
    entityEditor->setTitle(idx.data(qtDescriptivePhraseModel::TitleTextRole).toString());
    // TODO: editor should also allow adjusting entity type?
  }
}

void qtDescriptivePhraseDelegate::setModelData(
  QWidget* editor, QAbstractItemModel* model, const QModelIndex& idx) const
{
  smtk::extension::qtDescriptivePhraseEditor* entityEditor =
    qobject_cast<smtk::extension::qtDescriptivePhraseEditor*>(editor);
  if (entityEditor)
  {
    // TODO: editor should also allow adjusting entity type?
    model->setData(idx, entityEditor->title(), qtDescriptivePhraseModel::TitleTextRole);
  }
}

bool qtDescriptivePhraseDelegate::eventFilter(QObject* editor, QEvent* evt)
{
  if (evt->type() == QEvent::MouseButtonPress)
    return false;
  return QStyledItemDelegate::eventFilter(editor, evt);
}

367 368
std::string qtDescriptivePhraseDelegate::determineAction(
  const QPoint& pPos, const QModelIndex& idx, const QStyleOptionViewItem& option) const
369 370
{
  std::string res;
371 372 373 374 375
  if (m_visibilityMode)
  {
    res = "visible";
    return res;
  }
376 377 378 379 380 381 382 383 384 385 386 387 388
  // with the help of styles, return where the pPos is on:
  // the eye-ball, or the color swatch
  // visible icon
  QIcon visicon = qvariant_cast<QIcon>(idx.data(qtDescriptivePhraseModel::PhraseVisibilityRole));
  QSize visiconsize = visicon.actualSize(option.decorationSize);
  int px = pPos.x();
  int py = pPos.y();
  bool bvis = false, bcolor = false;
  if (!visicon.isNull())
  {
    bvis = px > option.rect.left() && px < (option.rect.left() + visiconsize.width()) &&
      py > option.rect.top() && py < (option.rect.top() + option.rect.height());
  }
389
  if (!bvis && idx.data(qtDescriptivePhraseModel::ColorMutableRole).toBool())
390 391
  {
    bcolor = px > (option.rect.left() + visiconsize.width() + 2) &&
392
      px < (option.rect.left() + visiconsize.width() + 2 + m_swatchSize) &&
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
      py > option.rect.top() && py < (option.rect.top() + option.rect.height());
  }

  if (bvis)
  {
    res = "visible";
  }
  else if (bcolor)
  {
    res = "color";
  }
  return res;
}

bool qtDescriptivePhraseDelegate::editorEvent(
  QEvent* evt, QAbstractItemModel* mod, const QStyleOptionViewItem& option, const QModelIndex& idx)
{
  bool res = this->QStyledItemDelegate::editorEvent(evt, mod, option, idx);
  if (evt->type() != QEvent::MouseButtonPress)
  {
    return res;
  }
  QMouseEvent* e = dynamic_cast<QMouseEvent*>(evt);
  if (!e || e->button() != Qt::LeftButton)
  {
    return res;
  }

421 422
  std::string whichIcon;
  whichIcon = this->determineAction(e->pos(), idx, option);
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

  if (whichIcon == "visible")
  {
    emit this->requestVisibilityChange(idx);
  }
  else if (whichIcon == "color")
  {
    emit this->requestColorChange(idx);
  }

  return res;
}

} // namespace model
} // namespace smtk