static void update_contact_methods(ChatView *self) { g_return_if_fail(IS_CHAT_VIEW(self)); ChatViewPrivate *priv = CHAT_VIEW_GET_PRIVATE(self); g_return_if_fail(priv->person); /* model for the combobox for the choice of ContactMethods */ auto cm_model = gtk_list_store_new( 1, G_TYPE_POINTER ); auto cms = priv->person->phoneNumbers(); for (int i = 0; i < cms.size(); ++i) { GtkTreeIter iter; gtk_list_store_append(cm_model, &iter); gtk_list_store_set(cm_model, &iter, 0, cms.at(i), -1); } gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_cm), GTK_TREE_MODEL(cm_model)); g_object_unref(cm_model); auto renderer = gtk_cell_renderer_text_new(); g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_cm), renderer, FALSE); gtk_cell_layout_set_cell_data_func( GTK_CELL_LAYOUT(priv->combobox_cm), renderer, (GtkCellLayoutDataFunc)render_contact_method, nullptr, nullptr); /* select the last used cm */ if (!cms.isEmpty()) { auto last_used_cm = cms.at(0); int last_used_cm_idx = 0; for (int i = 1; i < cms.size(); ++i) { auto new_cm = cms.at(i); if (difftime(new_cm->lastUsed(), last_used_cm->lastUsed()) > 0) { last_used_cm = new_cm; last_used_cm_idx = i; } } gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_cm), last_used_cm_idx); } /* show the combo box if there is more than one cm to choose from */ if (cms.size() > 1) gtk_widget_show_all(priv->combobox_cm); else gtk_widget_hide(priv->combobox_cm); }
bool validate(const FilePath& userHomePath, bool projectSharingEnabled) const { // ensure the scratch path and properties paths exist if (!scratchPath_.exists() || !propertiesPath_.exists()) return false; // ensure the properties are there if (project().empty() || workingDir().empty() || (lastUsed() == 0)) return false; // for projects validate that the base directory still exists std::string theProject = project(); if (theProject != kProjectNone) { FilePath projectDir = FilePath::resolveAliasedPath(theProject, userHomePath); if (!projectDir.exists()) return false; // check for project file FilePath projectPath = r_util::projectFromDirectory(projectDir); if (!projectPath.exists()) return false; // if we got this far the scope is valid, do one final check for // trying to open a shared project if sharing is disabled if (!projectSharingEnabled && r_util::isSharedPath(projectPath.absolutePath(), userHomePath)) return false; } // validated! return true; }
//least recently used int lru(int numFrames){ int turnCount = 0; int pagefaults = 0; frame *frameArr = malloc(sizeof(frame)*numFrames); int i = 0; int existsIndex = 0; int replaceIndex = 0; //initialize frames for(i = 0; i<numFrames; i++){ frameArr[i].occupied = 0; frameArr[i].turnOfEntry = -1; frameArr[i].frameNo = -1; frameArr[i].lastUsed = -1; } for(i = 0; i<pageRefLen; i++){ existsIndex = exists(randomPageRef[i],frameArr,numFrames); if(existsIndex>-1){ frameArr[existsIndex].lastUsed = turnCount; //printf("Used frame %d on turn %d\n",randomPageRef[i],turnCount); } else{ //add a page fault pagefaults++; //find the one to replace by finding the last used frame replaceIndex = lastUsed(frameArr,numFrames); //set it as occupies frameArr[replaceIndex].occupied = 1; //update its turn of entry frameArr[replaceIndex].turnOfEntry = turnCount; //update its frame number frameArr[replaceIndex].frameNo = randomPageRef[i]; //set its last used to the current turn count frameArr[replaceIndex].lastUsed = turnCount; //printf("Page fault on number %d at index %d\n",randomPageRef[i],replaceIndex); } turnCount++; } printf("Page faults for LRU for %d no of frames is : %d\n",numFrames,pagefaults); return 1; }
void ListViewSingleButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { // // This method paints a new row into the listview // painter->save(); // Set normal background color painter->setBrush((QColor("#ffffff"))); // Alternating rows if (index.row() % 2) { // Set brush to aliceblue painter->setBrush((QColor("#f5f5f5"))); } if(index.model()->data(index, Qt::UserRole + 4).toInt() == ConnectionState::Error) { painter->setBrush(QColor("#FFCCCC")); } QString textBottom (index.model()->data(index, Qt::UserRole + 101).toString()); QString textName (index.data(Qt::DisplayRole).toString()); // Hindergrund zeichnen painter->setPen(Qt::NoPen); if (textBottom == "__divider__") { painter->setBrush(QBrush(QColor("#f2f2f2"))); //0053a1 painter->drawRect(option.rect); painter->setPen(Qt::lightGray); painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); if (index.row() == 0) { painter->drawLine(option.rect.topLeft(), option.rect.topRight()); } painter->setPen(Qt::black); QStyleOptionViewItem nOptionD (option); QFont sdasda("Verdana", 10, QFont::Normal); //sdasda.setPixelSize(11); painter->setFont(sdasda); nOptionD.rect.adjust(3, 0, 0, 0); painter->drawText(nOptionD.rect, textName); rectForRow.insert(index.row(), QRect()); rectForRow[index.row()] = option.rect; painter->restore(); return; } painter->drawRect(option.rect); // Get text QString ip (index.data(Qt::UserRole + 1).toString()); quint32 lastUsed(index.data(Qt::UserRole + 2).toUInt()); quint32 lastConnected(index.data(Qt::UserRole + 3).toUInt()); // Set and calc items width int buttonWidth (36* windowsDpiScale()); int buttonHeight (36* windowsDpiScale()); int marginRight (10* windowsDpiScale()); int marginTop (5* windowsDpiScale()); // Draw delete button QStyleOptionViewItem buttonOption (option); // -10 is the right margin, the margin is necessary to reset the state when mouse is leaving to the right side buttonOption.rect.setRight(buttonWidth + 2 * marginRight); buttonOption.rect.setHeight(buttonHeight); buttonOption.rect.adjust(10* windowsDpiScale(), option.rect.height() / 2 - buttonHeight / 2, -(marginRight), marginTop + 1); buttonOption.rect.setHeight(buttonHeight); // Draw delete button and return limits // This is to figure out which button is under the mouse position rectForRow.insert(index.row(), QRect()); rectForRow[index.row()] = buttonOption.rect; auto drawButton = [](const QModelIndex &index, QPainter *painter, const QRect &rect, int role, ConnectionState conState) { // Draw delete button QStyleOptionButton buttonStyle; // // Set the button state to enabled buttonStyle.state |= QStyle::State_Enabled; // Set postion and size to the item buttonStyle.rect = rect; // Button is a flat button buttonStyle.features = QStyleOptionButton::Flat; // No text, only an icon // Icon default is 16x16 buttonStyle.iconSize = QSize(16* windowsDpiScale(), 16* windowsDpiScale()); // // Set the button states ButtonState state = (ButtonState) index.data(role).toInt(); QPen pen; pen.setWidth(1); switch(conState) { case ConnectionState::Disconnected: { if(state == Hover) { pen.setColor("#808080"); painter->setBrush(QBrush("#F0D151")); } else if(state == Pressed) { } else { pen.setColor("#808080"); painter->setBrush(QBrush("#EEEEEC")); } } break; case ConnectionState::Connecting: { if(state == Hover) { pen.setColor("#808080"); painter->setBrush(QBrush("#EEEEEC")); } else if(state == Pressed) { } else { pen.setColor("#808080"); painter->setBrush(QBrush("#F1C40F")); } } break; case ConnectionState::Connected: { if(state == Hover) { pen.setColor("#808080"); painter->setBrush(QBrush("#EEEEEC")); } else if(state == Pressed) { } else { pen.setColor("#808080"); painter->setBrush(QBrush("#72C02C")); } } break; case ConnectionState::Error: { if(state == Hover) { pen.setColor("#808080"); painter->setBrush(QBrush("#F0D151")); } else if(state == Pressed) { } else { pen.setColor("#808080"); painter->setBrush(QBrush("#FF0000")); } } break; default: { pen.setColor("#808080"); painter->setBrush(QBrush("#EEEEEC")); } } // Set the pen and draw the border of the button painter->setPen(pen); painter->drawRect(buttonStyle.rect); painter->setBrush(Qt::NoBrush); QRect buttonOption(rect); // Draw button QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonStyle, painter); }; ConnectionState conState = (ConnectionState)index.data(Qt::UserRole + 4).toInt(); drawButton(index, painter, buttonOption.rect, Qt::UserRole + 100, conState); // this->buttonLimits = this->drawButton(index, painter, buttonOption.rect, this->iconForType(this->buttonIconType, option), Qt::UserRole + 100); // Draw text QStyleOptionViewItem textOption (option); textOption.rect.adjust(buttonWidth + marginRight + 10 + 4, 4, buttonWidth + 2 * marginRight, 0); QPen pen; pen.setColor(QColor("#2c2c2c")); painter->setPen(pen); QFont font1("Verdana", 9, QFont::Normal); //font1.setPixelSize(11); QFont font2("Verdana", 7, QFont::Normal); //font2.setPixelSize(9); painter->setFont(font1); painter->drawText(textOption.rect, textName); textOption.rect.adjust(0, 13* windowsDpiScale(), 0, 0); painter->setFont(font2); //painter->drawText(textOption.rect, "Connected: "); //textOption.rect.adjust(0, 9, 0, 0); //painter->drawText(textOption.rect, "Last used: "); //textOption.rect.adjust(0, 9, 0, 0); typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period, std::ratio<24>>::type> days; std::chrono::seconds sec((QDateTime::currentDateTime().toTime_t() - lastConnected)); auto d = std::chrono::duration_cast<days>(sec); auto h = std::chrono::duration_cast<std::chrono::hours>(sec - d); auto m = std::chrono::duration_cast<std::chrono::minutes>((sec - d - h)); auto s = std::chrono::duration_cast<std::chrono::seconds>((sec - d - h - m)); QString day("%1%2 D "); day = day.arg(d.count() < 10 ? "0" : "") .arg(d.count()); QString hour("%1%2 H "); hour = hour.arg(h.count() < 10 ? "0" : "") .arg(h.count()); QString minute("%1%2 M "); minute = minute.arg(m.count() < 10 ? "0" : "") .arg(m.count()); switch(conState) { case ConnectionState::Connected: painter->drawText(textOption.rect, QObject::tr("Connected: ") + (day) + (hour) + (d.count() > 0 ? "" : minute)); break; case ConnectionState::Disconnected: painter->drawText(textOption.rect, QObject::tr("Last used: ") + (lastUsed == 0 ? QObject::tr("Never") : QDateTime::fromTime_t(lastUsed).toString("dd.MM.yy hh:mm"))); break; default: painter->drawText(textOption.rect, QObject::tr("Last used: ") + (lastUsed == 0 ? QObject::tr("Never") : QDateTime::fromTime_t(lastUsed).toString("dd.MM.yy hh:mm"))); break; }; if(buttonOption.rect.size() != _lastImgSize) { imgStart = imgStart_.scaled(buttonOption.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); imgOffline = imgOffline_.scaled(buttonOption.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); imgError = imgError_.scaled(buttonOption.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); imgOnline = imgOnline_.scaled(buttonOption.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); imgStop = imgStop_.scaled(buttonOption.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); imgConnecting = imgConnecting_.scaled(buttonOption.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); auto s = buttonOption.rect.size(); // I dont know why this thing has to be adjusted, but well, it works. s.setWidth(s.width() - 1); s.setHeight(s.height() - 1); movieKeks->setScaledSize(s); QSize size(16, 16); imgIconUsers = imgIconUsers_.scaled(size * windowsDpiScale(), Qt::KeepAspectRatio, Qt::SmoothTransformation); imgIconAutostart = imgIconAutostart_.scaled(size * windowsDpiScale(), Qt::KeepAspectRatio, Qt::SmoothTransformation); _lastImgSize = buttonOption.rect.size(); } buttonOption.rect.adjust(((buttonWidth) - imgStart.width()) / 2 +1, ((buttonWidth) - imgStart.height()) / 2 + 1, 0, 0); ButtonState state = (ButtonState) index.data(Qt::UserRole + 100).toInt(); switch(conState) { case ConnectionState::Disconnected: { if(state == Hover) painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgStart); else painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgOffline); } break; case ConnectionState::Connecting: { if(state == Hover) painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgStop); else { auto pixx = movieKeks->currentPixmap(); //auto size = imgStart.size(); ////size.setHeight(size.height() -10); //pixx = pixx.scaled(imgStart.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), pixx.toImage()); } } break; case ConnectionState::Connected: { if(state == Hover) painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgStop); else painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgOnline); } break; case ConnectionState::Error: { if(state == Hover) painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgStart); else painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgError); } break; default: { if(state == Hover) painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgStart); else painter->drawImage(buttonOption.rect.left(), buttonOption.rect.top(), imgOffline); } } static const int iconPadding = 5; textOption.rect.setTop(buttonOption.rect.bottom()); textOption.rect.adjust(0, -imgIconAutostart.height()+2, 0, 0); textOption.rect.setWidth(imgIconAutostart.width()); if(index.data(Qt::UserRole + 5).toBool()) { painter->drawImage(textOption.rect.left(), textOption.rect.top(), imgIconAutostart); textOption.rect.adjust(imgIconAutostart.width() + iconPadding, 0, 0, 0); } if(index.data(Qt::UserRole + 6).toBool()) { painter->drawImage(textOption.rect.left(), textOption.rect.top(), imgIconUsers); textOption.rect.adjust(imgIconAutostart.width() + iconPadding, 0, 0, 0); } painter->setPen(Qt::lightGray); painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); // Restore saved painter painter->restore(); }