void IconSelectButton::setCurrentIcon(const QString &iconString) { if ( m_currentIcon == iconString ) return; m_currentIcon = iconString; setText(QString()); setIcon(QIcon()); if ( iconString.size() == 1 ) { const QChar c = iconString[0]; if ( c.unicode() >= IconFirst && c.unicode() <= IconLast && QFontMetrics(iconFont()).inFont(c) ) { setFont(iconFont()); setText(iconString); } else { m_currentIcon = QString(); } } else if ( !iconString.isEmpty() ) { const QIcon icon(iconString); if ( icon.isNull() ) m_currentIcon = QString(); else setIcon(icon); } if (m_currentIcon.isEmpty()) { setFont(QFont()); setText( tr("...", "Select/browse icon.") ); } emit currentIconChanged(m_currentIcon); }
void IconSelectButton::setCurrentIcon(const QString &iconString) { if ( m_currentIcon == iconString ) return; m_currentIcon = iconString; setText(QString()); setIcon(QIcon()); if ( iconString.size() == 1 ) { const QChar c = iconString[0]; const ushort id = fixIconId( c.unicode() ); m_currentIcon = QString(QChar(id)); setFont(iconFont()); setText(m_currentIcon); } else if ( !iconString.isEmpty() ) { const QIcon icon(iconString); if ( icon.isNull() ) m_currentIcon = QString(); else setIcon(icon); } if (m_currentIcon.isEmpty()) { setFont(QFont()); setText( tr("...", "Select/browse icon.") ); } emit currentIconChanged(m_currentIcon); }
void IconFactory::drawIcon(ushort id, const QRect &itemRect, QPainter *painter) { QFont font = iconFont(); int size = qMin(itemRect.height() - 5, 18); font.setPixelSize(size); painter->save(); painter->setFont(font); painter->drawText( itemRect.right() - size, itemRect.top() + size, QString(QChar(id)) ); painter->restore(); }
QPixmap IconFactory::createPixmap(ushort id, const QColor &color, int size) { const int sz = (size > 0) ? size : iconSize; QPixmap pix(sz, sz); pix.fill(Qt::transparent); if (m_loaded) { QPainter painter(&pix); QFont font = iconFont(); font.setPixelSize(sz - 2); painter.setFont(font); painter.setPen(color); painter.drawText( QRect(1, 1, sz - 1, sz - 1), QString(QChar(id)) ); } return pix; }
const QPixmap &IconFactory::getPixmap(ushort id) { PixmapCache::iterator it = m_pixmapCache.find(id); if ( it == m_pixmapCache.end() ) { QPixmap pix(iconSize, iconSize); pix.fill(Qt::transparent); if ( isLoaded() ) { QPainter painter(&pix); painter.setFont( iconFont() ); painter.setPen(iconColor); painter.drawText( 0, 0, iconSize, iconSize, Qt::AlignHCenter | Qt::AlignVCenter, QString(QChar(id)) ); } it = m_pixmapCache.insert(id, pix); } return *it; }
void ItemEditorWidget::initMenuItems() { Q_ASSERT(m_editor); auto frame = qobject_cast<QFrame*>(m_editor); if (frame) frame->setFrameShape(QFrame::NoFrame); for (auto action : m_toolBar->actions()) delete action; QAction *act; act = new QAction( iconSave(), tr("Save"), m_editor ); m_toolBar->addAction(act); act->setToolTip( tr("Save Item (<strong>F2</strong>)") ); act->setShortcut( QKeySequence(tr("F2", "Shortcut to save item editor changes")) ); connect( act, &QAction::triggered, this, &ItemEditorWidget::saveAndExit ); act = new QAction( iconCancel(), tr("Cancel"), m_editor ); m_toolBar->addAction(act); act->setToolTip( tr("Cancel Editing and Revert Changes") ); act->setShortcut( QKeySequence(tr("Escape", "Shortcut to revert item editor changes")) ); connect( act, &QAction::triggered, this, &ItemEditorWidget::cancel ); auto doc = document(); if (document()) { m_toolBar->addSeparator(); act = new QAction( iconUndo(), tr("Undo"), m_editor ); m_toolBar->addAction(act); act->setShortcut(QKeySequence::Undo); act->setEnabled(false); connect( act, &QAction::triggered, doc, static_cast<void (QTextDocument::*)()>(&QTextDocument::undo) ); connect( doc, &QTextDocument::undoAvailable, act, &QAction::setEnabled ); act = new QAction( iconRedo(), tr("Redo"), m_editor ); m_toolBar->addAction(act); act->setShortcut(QKeySequence::Redo); act->setEnabled(false); connect( act, &QAction::triggered, doc, static_cast<void (QTextDocument::*)()>(&QTextDocument::redo) ); connect( doc, &QTextDocument::redoAvailable, act, &QAction::setEnabled ); m_toolBar->addSeparator(); act = new QAction( iconFont(), tr("Font"), m_editor ); m_toolBar->addAction(act); connect( act, &QAction::triggered, this, &ItemEditorWidget::setFont ); act = new QAction( iconBold(), tr("Bold"), m_editor ); m_toolBar->addAction(act); act->setShortcut( QKeySequence::Bold ); connect( act, &QAction::triggered, this, &ItemEditorWidget::toggleBoldText ); act = new QAction( iconItalic(), tr("Italic"), m_editor ); m_toolBar->addAction(act); act->setShortcut( QKeySequence::Italic ); connect( act, &QAction::triggered, this, &ItemEditorWidget::toggleItalicText ); act = new QAction( iconUnderline(), tr("Underline"), m_editor ); m_toolBar->addAction(act); act->setShortcut( QKeySequence::Underline ); connect( act, &QAction::triggered, this, &ItemEditorWidget::toggleUnderlineText ); act = new QAction( iconStrikethrough(), tr("Strikethrough"), m_editor ); m_toolBar->addAction(act); connect( act, &QAction::triggered, this, &ItemEditorWidget::toggleStrikethroughText ); m_toolBar->addSeparator(); act = new QAction( iconForeground(), tr("Foreground"), m_editor ); m_toolBar->addAction(act); connect( act, &QAction::triggered, this, &ItemEditorWidget::setForeground ); act = new QAction( iconBackground(), tr("Background"), m_editor ); m_toolBar->addAction(act); connect( act, &QAction::triggered, this, &ItemEditorWidget::setBackground ); m_toolBar->addSeparator(); act = new QAction( iconEraseStyle(), tr("Erase Style"), m_editor ); m_toolBar->addAction(act); connect( act, &QAction::triggered, this, &ItemEditorWidget::eraseStyle ); m_toolBar->addSeparator(); act = new QAction( iconSearch(), tr("Search"), m_editor ); act->setShortcuts(QKeySequence::Find); m_toolBar->addAction(act); connect( act, &QAction::triggered, this, &ItemEditorWidget::searchRequest ); } }
IconSelectDialog::IconSelectDialog(const QString &defaultIcon, QWidget *parent) : QDialog(parent) , m_iconList(new QListWidget(this)) , m_selectedIcon(defaultIcon) { setWindowTitle( tr("CopyQ Select Icon") ); m_iconList->setViewMode(QListView::IconMode); connect( m_iconList, SIGNAL(activated(QModelIndex)), this, SLOT(onIconListItemActivated(QModelIndex)) ); QFontMetrics fm( iconFont() ); const int gridSize = iconFontSizePixels() + 8; const QSize size(gridSize, gridSize); m_iconList->setFont( iconFont() ); m_iconList->setGridSize(size); m_iconList->setResizeMode(QListView::Adjust); m_iconList->setSelectionMode(QAbstractItemView::SingleSelection); m_iconList->setDragDropMode(QAbstractItemView::NoDragDrop); m_iconList->addItem( QString("") ); m_iconList->item(0)->setSizeHint(size); for (ushort i = IconFirst; i <= IconLast; ++i) { QChar c(i); if ( fm.inFont(c) ) { const QString icon(c); QListWidgetItem *item = new QListWidgetItem(icon, m_iconList); item->setSizeHint(size); if (defaultIcon == icon) m_iconList->setCurrentRow(m_iconList->count() - 1); } } QPushButton *browseButton = new QPushButton(tr("Browse..."), this); if ( m_selectedIcon.size() > 2 ) browseButton->setIcon(QIcon(m_selectedIcon)); connect( browseButton, SIGNAL(clicked()), this, SLOT(onBrowse()) ); QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); connect( buttonBox, SIGNAL(rejected()), this, SLOT(reject()) ); connect( buttonBox, SIGNAL(accepted()), this, SLOT(onAcceptCurrent()) ); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_iconList); QHBoxLayout *buttonLayout = new QHBoxLayout; layout->addLayout(buttonLayout); buttonLayout->addWidget(browseButton); buttonLayout->addWidget(buttonBox); m_iconList->setFocus(); // Restore previous geometry. restoreWindowGeometry(this, false); }