void ItemView::drawBackground(const Cairo::RefPtr<Cairo::Context>& cr, const int width, const int height) { //fill background if (isSelected()) { cr->set_source_rgb(1, 1, 1); cr->rectangle(0, 0, width, height); cr->fill(); } else { cr->set_source_rgb(0.98, 0.98, 0.98); cr->rectangle(0, 0, width, height); cr->fill(); } //time area Cairo::RefPtr<Cairo::LinearGradient> linearGradientTime = Cairo::LinearGradient::create(0, 0, 0, height); //set color by status ColorMode colorMode = getColorMode(); if (colorMode == COLOR_INACTIVE) { //gray linearGradientTime->add_color_stop_rgb(0, 0.75, 0.75, 0.75); linearGradientTime->add_color_stop_rgb(1, 0.65, 0.65, 0.65); } if (colorMode == COLOR_ALARM) { //orange linearGradientTime->add_color_stop_rgb(0, 1.00, 0.60, 0.30); linearGradientTime->add_color_stop_rgb(1, 0.90, 0.50, 0.20); } if (colorMode == COLOR_OK) { //green linearGradientTime->add_color_stop_rgb(0, 0.40, 0.70, 0.45); linearGradientTime->add_color_stop_rgb(1, 0.30, 0.60, 0.35); } cr->set_source(linearGradientTime); cr->rectangle(0, 0, TIME_WIDTH, height); cr->fill(); if (isSelected()) drawInnerShadow(cr, width, height); }
void MyWidget::drawWidget() { Painter p(this); if (m_color) { p.setSourceRGB(1.0, 0, 0); } else { p.setSourceRGB(0, 0, 0); } p.translate(minimumSize().width() / 2.0, minimumSize().height() / 2.0); p.setLineWidth(m_lineWidth); p.arc(0, 0, m_radius, 0, 2 * M_PI); p.save(); p.setSourceRGBA(1.0, 1.0, 1.0, 0.8); p.fillPreserve(); p.restore(); p.strokePreserve(); p.clip(); for (int i = 0; i < 12; ++i) { double inset = 30; p.save(); p.setLineCap(Painter::RoundLineCap); if(i % 3 != 0) { inset *= 0.8; p.setLineWidth(1.0); } p.moveTo((m_radius - inset) * cos (i * M_PI / 6.0), (m_radius - inset) * sin (i * M_PI / 6.0)); p.lineTo(m_radius * cos (i * M_PI / 6.0), m_radius * sin (i * M_PI / 6.0)); p.stroke(); p.restore(); } // store the current time time_t rawtime; time(&rawtime); struct tm * timeinfo = localtime (&rawtime); // compute the angles of the indicators of our clock double minutes = timeinfo->tm_min * M_PI / 30; double hours = timeinfo->tm_hour * M_PI / 6; double seconds= timeinfo->tm_sec * M_PI / 30; p.save(); p.setLineCap(Painter::RoundLineCap); // draw the seconds hand p.save(); p.setLineWidth(m_lineWidth); p.setSourceRGBA(0.7, 0.7, 0.7, 0.8); p.moveTo(0, 0); p.lineTo(sin(seconds) * (m_radius * 0.9), -cos(seconds) * (m_radius * 0.9)); p.stroke(); p.restore(); // draw the minutes hand p.setSourceRGBA(0.117, 0.337, 0.612, 0.9); p.moveTo(0, 0); p.lineTo(sin(minutes + seconds / 60.0) * (m_radius * 0.8), -cos(minutes + seconds / 60.0) * (m_radius * 0.8)); p.stroke(); // draw the hours hand p.setSourceRGBA(0.337, 0.612, 0.117, 0.9); p.moveTo(0, 0); p.lineTo(sin(hours + minutes / 12.0) * (m_radius * 0.5), -cos(hours + minutes / 12.0) * (m_radius * 0.5)); p.stroke(); p.restore(); p.setSourceRGBA(1, 0, 0, 0.5); p.arc(0, 0, m_lineWidth * 2.0, 0, 2.0 * M_PI); p.fill(); #if 0 // This is where we draw on the window Glib::RefPtr<Gdk::Window> window = get_window(); if(window) { Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); if(event) { // clip to the area indicated by the expose event so that we only // redraw the portion of the window that needs to be redrawn cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); cr->clip(); } // background gradient { Cairo::RefPtr<Cairo::LinearGradient> pat = Cairo::LinearGradient::create(0.0, 0.0, 0.0, height); pat->add_color_stop_rgb(1.0, 1.0, 1.0, 1.0); pat->add_color_stop_rgb(0.0, 0.0, 0.0, 0.0); cr->rectangle(0, 0, width, height); cr->set_source(pat); cr->fill(); } // scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e. // the center of the window cr->scale(width, height); cr->translate(0.5, 0.5); cr->set_line_width(m_line_width); cr->arc(0, 0, m_radius, 0, 2 * M_PI); cr->save(); cr->set_source_rgba(1.0, 1.0, 1.0, 0.8); cr->fill_preserve(); cr->restore(); cr->stroke_preserve(); cr->clip(); //clock ticks for (int i = 0; i < 12; i++) { double inset = 0.05; cr->save(); cr->set_line_cap(Cairo::LINE_CAP_ROUND); if(i % 3 != 0) { inset *= 0.8; cr->set_line_width(0.03); } cr->move_to( (m_radius - inset) * cos (i * M_PI / 6), (m_radius - inset) * sin (i * M_PI / 6)); cr->line_to ( m_radius * cos (i * M_PI / 6), m_radius * sin (i * M_PI / 6)); cr->stroke(); cr->restore(); /* stack-pen-size */ } // store the current time time_t rawtime; time(&rawtime); struct tm * timeinfo = localtime (&rawtime); // compute the angles of the indicators of our clock double minutes = timeinfo->tm_min * M_PI / 30; double hours = timeinfo->tm_hour * M_PI / 6; double seconds= timeinfo->tm_sec * M_PI / 30; cr->save(); cr->set_line_cap(Cairo::LINE_CAP_ROUND); // draw the seconds hand cr->save(); cr->set_line_width(m_line_width / 3); cr->set_source_rgba(0.7, 0.7, 0.7, 0.8); // gray cr->move_to(0, 0); cr->line_to(sin(seconds) * (m_radius * 0.9), -cos(seconds) * (m_radius * 0.9)); cr->stroke(); cr->restore(); // draw the minutes hand cr->set_source_rgba(0.117, 0.337, 0.612, 0.9); // blue cr->move_to(0, 0); cr->line_to(sin(minutes + seconds / 60) * (m_radius * 0.8), -cos(minutes + seconds / 60) * (m_radius * 0.8)); cr->stroke(); // draw the hours hand cr->set_source_rgba(0.337, 0.612, 0.117, 0.9); // green cr->move_to(0, 0); cr->line_to(sin(hours + minutes / 12.0) * (m_radius * 0.5), -cos(hours + minutes / 12.0) * (m_radius * 0.5)); cr->stroke(); cr->restore(); // draw a little dot in the middle cr->arc(0, 0, m_line_width / 3.0, 0, 2 * M_PI); cr->fill(); } #endif }
Cairo::RefPtr<Cairo::ImageSurface> TextSurface::create_cairo_surface(const std::string& text, const TextProperties& text_props, Cairo::TextExtents& out_text_extents, Cairo::FontExtents& out_font_extents) { { // get TextExtents and FontExtents Cairo::RefPtr<Cairo::ImageSurface> tmp_surface = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24, 0, 0); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(tmp_surface); cr->set_font_size(text_props.get_font_size()); cr->select_font_face(text_props.get_font(), Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->get_text_extents(text, out_text_extents); cr->get_font_extents(out_font_extents); } Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, static_cast<int>(out_text_extents.width + text_props.get_line_width()), static_cast<int>(out_text_extents.height + text_props.get_line_width())); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); // set the font cr->set_font_size(text_props.get_font_size()); cr->select_font_face(text_props.get_font(), Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); if (text_props.get_line_width() != 0) { // create path cr->move_to(-out_text_extents.x_bearing + text_props.get_line_width()/2.0, -out_text_extents.y_bearing + text_props.get_line_width()/2.0); cr->text_path(text); // paint cr->set_line_width(text_props.get_line_width()); cr->set_line_join(Cairo::LINE_JOIN_ROUND); cr->set_source_rgb(0.0, 0.0, 0.0); cr->stroke(); } // print text cr->move_to(-out_text_extents.x_bearing + text_props.get_line_width()/2.0, -out_text_extents.y_bearing + text_props.get_line_width()/2.0); cr->set_source_rgb(1.0, 1.0, 0.0); double y = -out_text_extents.y_bearing - out_font_extents.ascent; // toying around with color gradients if (false) { Cairo::RefPtr<Cairo::LinearGradient> gradient = Cairo::LinearGradient::create(0, y, 0, y + out_font_extents.ascent + out_font_extents.descent); gradient->add_color_stop_rgb(0.0, 1.0, 1.0, 0.0); gradient->add_color_stop_rgb(0.5, 1.0, 1.0, 1.0); gradient->add_color_stop_rgb(0.5, 0.4, 0.4, 0.2); gradient->add_color_stop_rgb(1.0, 1.0, 1.0, 0.0); cr->set_source(gradient); } cr->show_text(text); return surface; }
/** * \fn void Dessin::dessiner_carte () * \brief Méthode gérant le dessin de la carte de base. Elle est utilisée à chaque fois qu'il y a un évènement sur la zone de dessin * \param hauteur Hauteur sur laquelle la carte doit être dessinnée * \param largeur Largeur sur laquelle la carte doit être dessinnée * \param decalage_y Décalage en ordonnée quand il y a un zoom. Cela permet d'afficher la zone voulue * \param decalage_x Décalage en abscisse quand il y a un zoom. Cela permet d'afficher la zone voulue * \return Rien */ void Dessin::dessiner_carte(int hauteur, int largeur, int decalage_y, int decalage_x){ /* Cette variable permet la converion de nombres en lettres */ stringstream *sstr; /* Cette variable permet de connaitre la taille (hauteur, largeur) d'une phrase */ Cairo::TextExtents extents; double premier_point = 0; double **points; double **croisement; double abscisse_tmp, ordonnee_tmp; /* Fond d'écran */ cr = this->get_window()->create_cairo_context(); /* La couleur est le blanc */ cr->set_source_rgb(1.0, 1.0, 1.0); cr->rectangle(0, 0, L_MAP, H_MAP); cr->fill(); /* La couleur est le noir */ cr->set_source_rgb(0.0, 0.0, 0.0); cr->set_line_width(1.0); /* Lecture du fichier contenant la carte */ points = lire_carte(hauteur, largeur, chemin_carte); /* Gestion d'erreur de lecture du fichier SHP */ if(points[3][0]!=0){ for(int i=0; i<points[3][0]; i++){ if(premier_point == points[2][i]){ cr->line_to(points[0][i] - decalage_x, points[1][i] - decalage_y); } else{ premier_point = points[2][i]; } cr->move_to(points[0][i] - decalage_x, points[1][i] - decalage_y); } cr->stroke(); } int ordonnees[194]; for(int j=0; j<194; j++){ ordonnees[j] = (ordonnee_points[196*j] - points[3][5]); } croisement = points_dans_polygone(ordonnees, chemin_carte); if(initialisation == 1){ valeur_min = matrice.minCoeff(); valeur_max = matrice.maxCoeff(); initialisation = 0; } else{ if(matrice.minCoeff() < valeur_min) valeur_min = matrice.minCoeff(); if(matrice.maxCoeff() > valeur_max) valeur_max = matrice.maxCoeff(); } for(int i=0; i<194; i++){ for(int j=0; j<196; j++){ int ecrire = 0; for(int k=0; k<100; k++){ if(croisement[i][k] != 0){ if((abscisse_points[(i*196)+j] >= croisement[i][k]) ){ if(ecrire == 0 ) ecrire = 1; else ecrire = 0; } } else{ k = 100; } } if(ecrire == 1 ){ cr->set_source_rgb(gradient_couleur(valeur_min, valeur_max, matrice(i,j), 'R'), gradient_couleur(valeur_min, valeur_max, matrice(i,j), 'G'), gradient_couleur(valeur_min, valeur_max, matrice(i,j), 'B')); ordonnee_tmp = ordonnee_points[(i*196)+j] - (points[3][5] - points[3][3]); abscisse_tmp = ((abscisse_points[(i*196)+j] - points[3][1]) * largeur)/(points[3][2] - points[3][1]); ordonnee_tmp = ((ordonnee_tmp - points[3][3]) * hauteur)/(points[3][4] - points[3][3]); cr->arc (abscisse_tmp - decalage_x, ordonnee_tmp - decalage_y, 1, 0, 2 * M_PI); cr->fill(); } } } /* Insertion de texte pour mettre un titre à la carte */ /* Le titre est écrit en noir */ cr->set_source_rgb(0, 0, 0); /* Les lettres font 15 pixels de hauteur */ cr->set_font_size(15); /* Sélection de la police */ cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD); string deb_titre("Carte "); cr->get_text_extents(deb_titre + titre.c_str(), extents); /* Positionnement du titre */ cr->move_to((L_MAP/2) - (extents.width/2), H_MAP - 20); /* Ecriture du titre */ cr->show_text(deb_titre + titre.c_str()); /* Dessin du gradient de couleur */ /* Création d'un gradient le long de la ligne définie par (L_MAP - 50, 50) et (L_MAP - 50, H_MAP - 100) */ Cairo::RefPtr< Cairo::LinearGradient > gradient = Cairo::LinearGradient::create(L_MAP - 50, 50, L_MAP - 50, H_MAP - 100); /* Couleur de départ pour le gradient */ gradient->add_color_stop_rgb(0, 0, 1, 0); /* Couleur de fin pour le gradient */ if(valeur_min == valeur_max) gradient->add_color_stop_rgb(1, 0, 1, 0); else gradient->add_color_stop_rgb(1, 1, 0, 0); /* Dessin d'un rectangle */ cr->rectangle(L_MAP - 50, 50, 10, H_MAP - 100); /* Colorisation du rectangle avec le gradient */ cr->set_source(gradient); /* Remplissage du rectangle (toujours avec le gradient) */ cr->fill(); /* Conversion du minimum de la matrice en lettres (pour afficher ce que représente la valeur du minimum du gradient)*/ sstr = new stringstream(); *sstr << valeur_min; /* Le titre est écrit en noir */ cr->set_source_rgb(0, 0, 0); /* Les lettres font 10 pixels de hauteur */ cr->set_font_size(10); /* Sélection de la police */ cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD); /* Récupération de la taille la valeur min(hauteur et largeur) */ cr->get_text_extents((sstr->str()).c_str(), extents); /* Positionnement du texte */ cr->move_to((L_MAP - 45) - (extents.width/2), 50 - 5); /* Ecriture du titre */ cr->show_text((sstr->str()).c_str()); /* Conversion du maximum de la matrice en lettres (pour afficher ce que représente la valeur du maximum du gradient)*/ sstr = new stringstream(); *sstr << valeur_max; /* Récupération de la taille la valeur max(hauteur et largeur) */ cr->get_text_extents((sstr->str()).c_str(), extents); /* Positionnement du texte */ cr->move_to((L_MAP - 45) - (extents.width/2), H_MAP - 50 + extents.height + 5); /* Ecriture du titre */ cr->show_text((sstr->str()).c_str()); /* Vidage du stringstream */ if(zoom_actif == 0) get_gdk_pixbuf(); }