void MainWindow::createStatusBar() { m_exportPathLabel = new QLabel(this); m_exportPathLabel->setMinimumWidth(200); m_exportPathLabel->setText(EXPORT_TO_LABEL_FORMAT_STRING.arg("")); m_exportPathLabel->setEnabled(false); m_currentImageFormatLabel = new QLabel(this); QFontMetrics font_metrics(m_exportPathLabel->font()); m_cacheSizeLabel = new QLabel(this); m_cacheSizeLabel->setMinimumWidth(font_metrics.width(QString(CACHE_SIZE_FORMAT_STRING.arg("#.## Mb")))); m_autoRefreshLabel = new QLabel(this); m_autoRefreshLabel->setText(AUTOREFRESH_STATUS_LABEL); #ifdef Q_WS_X11 const int label_fram_style = QFrame::Panel | QFrame::Sunken; m_exportPathLabel->setFrameStyle(label_fram_style); m_currentImageFormatLabel->setFrameStyle(label_fram_style); m_cacheSizeLabel->setFrameStyle(label_fram_style); m_autoRefreshLabel->setFrameStyle(label_fram_style); #endif statusBar()->addPermanentWidget(m_exportPathLabel); statusBar()->addPermanentWidget(m_cacheSizeLabel); statusBar()->addPermanentWidget(m_autoRefreshLabel); statusBar()->addPermanentWidget(m_currentImageFormatLabel); statusBar()->showMessage(tr("Ready"), STATUSBAR_TIMEOUT); }
Pane::Pane(QWidget *parent) : QWidget(parent), ui(new Ui::Pane) { directory = 0; pending_directory = 0; ui->setupUi(this); ui->list->setModel(&file_list_model); ui->list->installEventFilter(this); ui->list->viewport()->installEventFilter(this); ui->list->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); QFontMetrics font_metrics(ui->list->font()); ui->list->verticalHeader()->setFixedWidth(10 + font_metrics.width(">")); ui->address->installEventFilter(this); ready = true; main_window = 0; connect(ui->address, SIGNAL(returnPressed()), this, SLOT(on_go_clicked())); ui->loading_indicator->hide(); QMovie* loading_movie = new QMovie(":/loading.gif", QByteArray(), ui->loading_indicator); ui->loading_indicator->setMovie(loading_movie); loading_movie->start(); connect(ui->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(current_index_changed(QModelIndex,QModelIndex))); connect(ui->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SIGNAL(selection_changed())); connect(ui->list->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SIGNAL(selection_changed())); }
void ContactListEdit::addContactEntry(const QString& contactText, const bts::addressbook::contact& c) { QFont default_font; default_font.setPointSize( default_font.pointSize() - 1 ); QFontMetrics font_metrics(default_font); QRect bounding = font_metrics.boundingRect(contactText); int completion_width = font_metrics.width(contactText); int completion_height = bounding.height(); completion_width += 20; QImage completion_image(completion_width, completion_height + 4, QImage::Format_ARGB32); completion_image.fill(QColor(0, 0, 0, 0) ); QPainter painter; painter.begin(&completion_image); painter.setFont(default_font); painter.setRenderHint(QPainter::Antialiasing); QBrush brush(Qt::SolidPattern); brush.setColor( QColor( 205, 220, 241 ) ); QPen pen; bool isKeyhoteeFounder = Contact::isKeyhoteeFounder(c); if (isKeyhoteeFounder) { QLinearGradient grad(QPointF(0, 0), QPointF(0, 1)); grad.setCoordinateMode(QGradient::ObjectBoundingMode); grad.setColorAt(0.3, QColor(231, 190, 66)); grad.setColorAt(1.0, QColor(103, 51, 1)); brush = QBrush(grad); pen.setColor( QColor( 103, 51, 1 ) ); } else { brush.setColor( QColor( 205, 220, 241 ) ); pen.setColor( QColor( 105,110,180 ) ); } painter.setBrush(brush); painter.setPen(pen); painter.drawRoundedRect(0, 0, completion_width - 1, completion_image.height() - 1, 8, 8, Qt::AbsoluteSize); painter.setPen(QPen()); painter.drawText(QPoint(10, completion_height - 2), contactText); QTextCursor text_cursor = textCursor(); uint32_t prefix_len = _completer->completionPrefix().length(); for (uint32_t i = 0; i < prefix_len; ++i) text_cursor.deletePreviousChar(); // int extra = completion.length() - // tc.movePosition(QTextCursor::Left); // tc.movePosition(QTextCursor::EndOfWord); // tc.insertText(completion.right(extra)); text_cursor.insertImage(completion_image, contactText); text_cursor.insertText(" "); setTextCursor(text_cursor); }
void ParamWidget::SetPrecision(const QString& name, int digits, int decimal_places) { QWidget* widget = GetWidget(name); QSlider* slider = dynamic_cast<QSlider*>(widget); if (slider) { QLabel* label = slider->property("param_widget_label").value<QLabel*>(); if (label) { const QFont& font = label->font(); QFontMetrics font_metrics(font); QString sample_text; for (int i = 0; i < digits; ++i) { sample_text.append('9'); } if (decimal_places) { sample_text.append('.'); } const int width = font_metrics.width(sample_text); QString format_str; format_str.sprintf("%%%d.%df", digits, decimal_places); label->setProperty("format_str", format_str); label->setFixedWidth(width); } } }
void Layout::draw(QPainter* painter, float width, float height) { if (hasBackgroundColor()) painter->fillRect(0, 0, width, height, getBackgroundColor()); if (background_image) { if (width == background_image->width() && height == background_image->height()) painter->drawPixmap(QRectF(0, 0, width, height), *background_image, QRectF(0, 0, background_image->width(), background_image->height())); else { if (!background_image_cache || background_image_cache->width() != width || background_image_cache->height() != height) { delete background_image_cache; background_image_cache = new QPixmap(width, height); QPainter cache_painter(background_image_cache); cache_painter.setRenderHint(QPainter::SmoothPixmapTransform, true); cache_painter.drawPixmap(QRectF(0, 0, width, height), *background_image, QRectF(0, 0, background_image->width(), background_image->height())); cache_painter.setRenderHint(QPainter::SmoothPixmapTransform, false); } painter->drawPixmap(QRectF(0, 0, width, height), *background_image_cache, QRectF(0, 0, background_image_cache->width(), background_image_cache->height())); } } for (std::map<QString, Point*>::iterator it = points.begin(); it != points.end(); ++it) { Point* point = it->second; Font* font_data = point->strong_font ? point->font_strong : point->font_normal; QFont font = qFontFromFont(font_data, height); QFontMetricsF font_metrics(font); // NOTE: Could optimize this by caching the size float text_width = font_metrics.width(point->text); float text_height = font_metrics.height(); // TODO: Count lines QRectF rect = QRectF(point->pos.x()*width - 0.5f*text_width, point->pos.y()*height - 0.5f*text_height, text_width, text_height); Qt::Alignment alignment = point->alignment; if (point->alignment & Qt::AlignLeft) rect.translate(0.5f*text_width, 0); else if (point->alignment & Qt::AlignRight) rect.translate(-0.5f*text_width, 0); if (point->alignment & Qt::AlignTop) rect.translate(0, 0.5f*text_height); else if (point->alignment & Qt::AlignBottom) rect.translate(0, -0.5f*text_height); else if (point->alignment & Qt::AlignAbsolute) { rect.translate(0, 0.5f*text_height - font_metrics.ascent()); alignment = (alignment & ~Qt::AlignAbsolute) | Qt::AlignVCenter; } painter->setFont(font); painter->setPen(font_data->color); if (point->max_width > 0) painter->drawText(rect, alignment, font_metrics.elidedText(point->text, Qt::ElideMiddle, point->max_width*width)); else painter->drawText(rect, alignment, point->text); } }
Font Font::load(Canvas &canvas, const std::string &family_name, const FontDescription &reference_desc, FontFamily &font_family, const XMLResourceDocument &doc, std::function<Resource<Sprite>(Canvas &, const std::string &)> cb_get_sprite) { DomElement font_element; XMLResourceNode resource; resource = doc.get_resource(family_name); font_element = resource.get_element(); DomElement sprite_element = font_element.named_item("sprite").to_element(); if (!sprite_element.is_null()) { if (!sprite_element.has_attribute("glyphs")) throw Exception(string_format("Font resource %1 has no 'glyphs' attribute.", resource.get_name())); if (!sprite_element.has_attribute("letters")) throw Exception(string_format("Font resource %1 has no 'letters' attribute.", resource.get_name())); if (!cb_get_sprite) throw Exception(string_format("Font resource %1 requires a sprite loader callback specified.", resource.get_name())); Resource<Sprite> spr_glyphs = cb_get_sprite(canvas, sprite_element.get_attribute("glyphs")); const std::string &letters = sprite_element.get_attribute("letters"); int spacelen = StringHelp::text_to_int(sprite_element.get_attribute("spacelen", "-1")); bool monospace = StringHelp::text_to_bool(sprite_element.get_attribute("monospace", "false")); // Modify the default font metrics, if specified float height = 0.0f; float line_height = 0.0f; float ascent = 0.0f; float descent = 0.0f; float internal_leading = 0.0f; float external_leading = 0.0f; if (sprite_element.has_attribute("height")) height = StringHelp::text_to_float(sprite_element.get_attribute("height", "0")); if (sprite_element.has_attribute("line_height")) line_height = StringHelp::text_to_float(sprite_element.get_attribute("line_height", "0")); if (sprite_element.has_attribute("ascent")) ascent = StringHelp::text_to_float(sprite_element.get_attribute("ascent", "0")); if (sprite_element.has_attribute("descent")) descent = StringHelp::text_to_float(sprite_element.get_attribute("descent", "0")); if (sprite_element.has_attribute("internal_leading")) internal_leading = StringHelp::text_to_float(sprite_element.get_attribute("internal_leading", "0")); if (sprite_element.has_attribute("external_leading")) external_leading = StringHelp::text_to_float(sprite_element.get_attribute("external_leading", "0")); FontMetrics font_metrics(height, ascent, descent, internal_leading, external_leading, line_height, canvas.get_pixel_ratio()); font_family.add(canvas, spr_glyphs.get(), letters, spacelen, monospace, font_metrics); FontDescription desc = reference_desc.clone(); return Font(font_family, desc); } DomElement ttf_element = font_element.named_item("ttf").to_element(); if (ttf_element.is_null()) ttf_element = font_element.named_item("freetype").to_element(); if (!ttf_element.is_null()) { FontDescription desc = reference_desc.clone(); std::string filename; if (ttf_element.has_attribute("file")) { filename = PathHelp::combine(resource.get_base_path(), ttf_element.get_attribute("file")); } if (!ttf_element.has_attribute("typeface")) throw Exception(string_format("Font resource %1 has no 'typeface' attribute.", resource.get_name())); std::string font_typeface_name = ttf_element.get_attribute("typeface"); if (ttf_element.has_attribute("height")) desc.set_height(ttf_element.get_attribute_int("height", 0)); if (ttf_element.has_attribute("average_width")) desc.set_average_width(ttf_element.get_attribute_int("average_width", 0)); if (ttf_element.has_attribute("anti_alias")) desc.set_anti_alias(ttf_element.get_attribute_bool("anti_alias", true)); if (ttf_element.has_attribute("subpixel")) desc.set_subpixel(ttf_element.get_attribute_bool("subpixel", true)); if (filename.empty()) { font_family.add(font_typeface_name, desc); return Font(font_family, desc); } else { font_family.add(desc, filename, resource.get_file_system()); return Font(font_family, desc); } } throw Exception(string_format("Font resource %1 did not have a <sprite> or <ttf> child element", resource.get_name())); }
void ContactListEdit::addContactEntry(const QString& contactText, const bts::addressbook::contact& c) { QFont default_font; default_font.setPointSize( default_font.pointSize() - 1 ); QFontMetrics font_metrics(default_font); QRect bounding = font_metrics.boundingRect(contactText); int completion_width = font_metrics.width(contactText); int completion_height = bounding.height(); completion_width += 20; QImage completion_image(completion_width, completion_height + 4, QImage::Format_ARGB32); completion_image.fill(QColor(0, 0, 0, 0) ); QPainter painter; painter.begin(&completion_image); painter.setFont(default_font); painter.setRenderHint(QPainter::Antialiasing); QBrush brush(Qt::SolidPattern); brush.setColor( QColor( 205, 220, 241 ) ); QPen pen; bool isKeyhoteeFounder = Contact::isKeyhoteeFounder(c); if (isKeyhoteeFounder) { QLinearGradient grad(QPointF(0, 0), QPointF(0, 1)); grad.setCoordinateMode(QGradient::ObjectBoundingMode); grad.setColorAt(0, QColor(35, 40, 3)); grad.setColorAt(0.102273, QColor(136, 106, 22)); grad.setColorAt(0.225, QColor(166, 140, 41)); grad.setColorAt(0.285, QColor(204, 181, 74)); grad.setColorAt(0.345, QColor(235, 219, 102)); grad.setColorAt(0.415, QColor(245, 236, 112)); grad.setColorAt(0.52, QColor(209, 190, 76)); grad.setColorAt(0.57, QColor(187, 156, 51)); grad.setColorAt(0.635, QColor(168, 142, 42)); grad.setColorAt(0.695, QColor(202, 174, 68)); grad.setColorAt(0.75, QColor(218, 202, 86)); grad.setColorAt(0.815, QColor(208, 187, 73)); grad.setColorAt(0.88, QColor(187, 156, 51)); grad.setColorAt(0.935, QColor(137, 108, 26)); grad.setColorAt(1, QColor(35, 40, 3)); brush = QBrush(grad); pen.setColor( QColor( 103, 51, 1 ) ); } else { brush.setColor( QColor( 205, 220, 241 ) ); pen.setColor( QColor( 105,110,180 ) ); } painter.setBrush(brush); painter.setPen(pen); painter.drawRoundedRect(0, 0, completion_width - 1, completion_image.height() - 1, 8, 8, Qt::AbsoluteSize); painter.setPen(QPen()); painter.drawText(QPoint(10, completion_height - 2), contactText); QTextDocument* doc = document(); doc->addResource(QTextDocument::ImageResource, QUrl(contactText), completion_image); QTextImageFormat format; format.setName(contactText); encodePublicKey(c.public_key, &format); QTextCursor txtCursor = textCursor(); txtCursor.insertImage(format); txtCursor.insertText(" "); setTextCursor(txtCursor); }
std::string Font::clipped_text(const CanvasPtr &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text) { std::string out_string; out_string.reserve(text.length()); Pointf pos; FontMetrics fm = font_metrics(canvas); float descent = fm.descent(); float line_spacing = fm.line_height(); std::vector<std::string> lines = Text::split(text, "\n", false); for (std::vector<std::string>::size_type i = 0; i < lines.size(); i++) { if (i == 0 || pos.y + descent < box_size.height) { Sizef size = measure_text(canvas, lines[i]).bbox_size; if (pos.x + size.width <= box_size.width) { if (!out_string.empty()) out_string += "\n"; out_string += lines[i]; } else { Sizef ellipsis = measure_text(canvas, ellipsis_text).bbox_size; int seek_start = 0; int seek_end = lines[i].size(); int seek_center = (seek_start + seek_end) / 2; UTF8_Reader utf8_reader(lines[i].data(), lines[i].length()); while (true) { utf8_reader.set_position(seek_center); utf8_reader.move_to_leadbyte(); if (seek_center != utf8_reader.position()) utf8_reader.next(); seek_center = utf8_reader.position(); if (seek_center == seek_end) break; utf8_reader.set_position(seek_start); utf8_reader.next(); if (utf8_reader.position() == seek_end) break; Sizef text_size = measure_text(canvas, lines[i].substr(0, seek_center)).bbox_size; if (pos.x + text_size.width + ellipsis.width >= box_size.width) seek_end = seek_center; else seek_start = seek_center; seek_center = (seek_start + seek_end) / 2; } if (!out_string.empty()) out_string += "\n"; out_string += lines[i].substr(0, seek_center) + ellipsis_text; } pos.y += line_spacing; } } return out_string; }