1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include "widget.h" #include <QPixmap> #include <QDebug> #include <Qcursor> #include <QMetaEnum> Widget::Widget(QWidget *parent) : QWidget(parent), timer(new QTimer(this)), menu(new QMenu(this)) { this->setWindowFlag(Qt::FramelessWindowHint); this->setAttribute(Qt::WA_TranslucentBackground); this->installEventFilter(new DragFilter);
connect(timer,&QTimer::timeout,this,[this](){ static int index=0; auto paths=this->action_map.value(this->cur_role_act); this->cur_role_pix=paths[index++%paths.size()]; this->update();
}); loadRoleActRes();
showActAnimation(RoleAct::SayHello); initMennu(); }
Widget::~Widget() {}
void Widget::showActAnimation(RoleAct k) { timer->stop(); this->cur_role_act=k; timer->start(500);
}
void Widget::onMenuTriggered(QAction *action) { QMetaEnum me=QMetaEnum::fromType<RoleAct>(); bool ok; int k=me.keyToValue(action->text().toStdString().c_str(),&ok); if(!ok)return; showActAnimation(static_cast<RoleAct>(k)); }
void Widget::paintEvent(QPaintEvent *event) { QPainter painter(this); QPixmap pix;
pix.load(this->cur_role_pix.toLocalFile()); int targetWidth = 200; int targetHeight = 150; pix = pix.scaled(targetWidth, targetHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation); painter.drawPixmap(0,0,pix);
}
void Widget::contextMenuEvent(QContextMenuEvent *event) { this->menu->popup(QCursor::pos()); }
void Widget::loadRoleActRes() { auto addRes=[this](RoleAct k,QString path,int count) { QList<QUrl>paths; char buf[260]; for(int i=0;i<count;++i) { memset(buf,0,sizeof(buf)); sprintf_s(buf,path.toStdString().c_str(),i); paths.append(QUrl::fromLocalFile(buf)); } action_map.insert(k,paths);
}; addRes(RoleAct::SayHello,":/pic/image/sayHello/sayHello%d.png",4); addRes(RoleAct::Sleep,":/%d.png",32); addRes(RoleAct::Swing,":/%d.png",11);
}
void Widget::initMennu() { menu->addAction("SayHello"); menu->addAction("Sleep"); menu->addAction("Swing");
QAction* act=new QAction("Hide"); connect(act,&QAction::triggered,this,[this](){ this->setVisible(false); });
menu->addAction(act); connect(this->menu,&QMenu::triggered,this,Widget::onMenuTriggered); }
|