Skip to content

Commit

Permalink
add dmemory-warning-dialog
Browse files Browse the repository at this point in the history
Change-Id: I0e4b87e917048a69be9985e55a0013b270ceb7fb
  • Loading branch information
sbwtw committed Jan 8, 2018
1 parent a587d21 commit 0b50c15
Show file tree
Hide file tree
Showing 17 changed files with 604 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ debug
release
build*

*.pro.user*
*.user*
*.qm

misc/*/deepin-toggle-desktop.desktop
3 changes: 2 additions & 1 deletion dde-session-ui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ SUBDIRS += dde-shutdown \
dde-warning-dialog \
dde-welcome \
dde-wm-chooser \
dde-dman-portal
dde-dman-portal \
dmemory-warning-dialog

include(common.pri)
contains(DEFINES, ARCH_MIPSEL) {
Expand Down
39 changes: 39 additions & 0 deletions dmemory-warning-dialog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.7)

project(dmemory-warning-dialog
VERSION 1.0.0)

set(BIN_NAME dmemory-warning-dialog)
#set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_FLAGS -Wall)

if (NOT (${CMAKE_BUILD_TYPE} MATCHES "Debug"))
set(CMAKE_CXX_FLAGS -O3)
endif ()

# Sources files
file(GLOB SRCS "src/*.h" "src/*.cpp")

# Find the library
find_package(PkgConfig REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Widgets Concurrent X11Extras DBus)
find_package(Dtk REQUIRED COMPONENTS Widget)

pkg_check_modules(XLIBS REQUIRED x11)

# driver-manager
add_executable(${BIN_NAME} ${SRCS})
target_include_directories(${BIN_NAME} PUBLIC ${XLIBS_INCLUDE_DIRS} ${Qt5X11Extras_INCLUDE_DIRS})
target_link_libraries(${BIN_NAME} PRIVATE
${DtkWidget_LIBRARIES}
${Qt5Widgets_LIBRARIES}
${Qt5Concurrent_LIBRARIES}
${Qt5DBus_LIBRARIES}
${Qt5X11Extras_LIBRARIES}
${XLIBS_LIBRARIES}
)

27 changes: 27 additions & 0 deletions dmemory-warning-dialog/dmemory-warning-dialog.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
QT += core gui widgets dbus x11extras

TARGET = dmemory-warning-dialog
TEMPLATE = app

CONFIG += c++14 link_pkgconfig
PKGCONFIG += dtkwidget

HEADERS += \
src/buttondelegate.h \
src/dmemorywarningdialog.h \
src/processinfodelegate.h \
src/processinfomanager.h \
src/processinfomodel.h \
src/processinfotable.h

SOURCES += \
src/buttondelegate.cpp \
src/dmemorywarningdialog.cpp \
src/main.cpp \
src/processinfodelegate.cpp \
src/processinfomanager.cpp \
src/processinfomodel.cpp \
src/processinfotable.cpp

target.path = /usr/bin
INSTALLS += target
48 changes: 48 additions & 0 deletions dmemory-warning-dialog/src/buttondelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "buttondelegate.h"
#include "processinfomodel.h"

#include <QApplication>
#include <QProcess>
#include <QDebug>

void terminate(const QStringList &pidList)
{
for (const auto &pid : pidList)
QProcess::startDetached("kill", QStringList() << pid);
}

ButtonDelegate::ButtonDelegate(QObject *parent)
: QItemDelegate(parent)
{

}

void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
button.rect = option.rect.marginsRemoved(QMargins(6, 6, 6, 6));
button.text = index.data().toString();
button.state = QStyle::State_Enabled;
if (index.data(ProcessInfoModel::StateRole).toBool())
button.state |= QStyle::State_Sunken | QStyle::State_Active;

QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
}

bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
Q_UNUSED(option);

switch (event->type())
{
case QEvent::MouseButtonPress:
model->setData(index, QVariant(), ProcessInfoModel::StateRole);
break;
case QEvent::MouseButtonRelease:
terminate(index.data(ProcessInfoModel::PidListRole).value<QStringList>());
break;
default:;
}

return true;
}
17 changes: 17 additions & 0 deletions dmemory-warning-dialog/src/buttondelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H

#include <QItemDelegate>

class ButtonDelegate : public QItemDelegate
{
Q_OBJECT

public:
explicit ButtonDelegate(QObject *parent = nullptr);

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};

#endif // BUTTONDELEGATE_H
33 changes: 33 additions & 0 deletions dmemory-warning-dialog/src/dmemorywarningdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "dmemorywarningdialog.h"
#include "processinfotable.h"
#include "processinfomodel.h"
#include "processinfodelegate.h"
#include "buttondelegate.h"

#include <QKeyEvent>
#include <QApplication>
#include <QScreen>

DMemoryWarningDialog::DMemoryWarningDialog(QWidget *parent)
: QMainWindow(parent)
{
ProcessInfoTable *table = new ProcessInfoTable;
table->setModel(new ProcessInfoModel);
table->setItemDelegate(new ProcessInfoDelegate);
table->setItemDelegateForColumn(3, new ButtonDelegate);

setCentralWidget(table);
setFixedSize(400, 600);
move(qApp->primaryScreen()->geometry().center() - rect().center());
}

void DMemoryWarningDialog::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
#ifdef QT_DEBUG
case Qt::Key_Escape: qApp->quit(); break;
#endif
default:;
}
}
17 changes: 17 additions & 0 deletions dmemory-warning-dialog/src/dmemorywarningdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef DMEMORYWARNINGDIALOG_H
#define DMEMORYWARNINGDIALOG_H

#include <QMainWindow>

class DMemoryWarningDialog : public QMainWindow
{
Q_OBJECT

public:
explicit DMemoryWarningDialog(QWidget *parent = nullptr);

private:
void keyPressEvent(QKeyEvent *e);
};

#endif // DMEMORYWARNINGDIALOG_H
18 changes: 18 additions & 0 deletions dmemory-warning-dialog/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include "dmemorywarningdialog.h"

#include <DApplication>

DWIDGET_USE_NAMESPACE

int main(int argc, char *args[])
{
DApplication::loadDXcbPlugin();
DApplication dapp(argc, args);
dapp.setAttribute(Qt::AA_UseHighDpiPixmaps);

DMemoryWarningDialog dialog;
dialog.show();

return dapp.exec();
}
21 changes: 21 additions & 0 deletions dmemory-warning-dialog/src/processinfodelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "processinfodelegate.h"
#include "processinfomodel.h"

#include <QApplication>
#include <QDebug>
#include <QPainter>

ProcessInfoDelegate::ProcessInfoDelegate(QObject *parent)
: QItemDelegate(parent)
{

}

void ProcessInfoDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() != COLUMN_ICON)
return QItemDelegate::paint(painter, option, index);

const QPixmap pix = index.data(ProcessInfoModel::IconRole).value<QPixmap>();
painter->drawPixmap(option.rect.center() - pix.rect().center() / pix.devicePixelRatio(), pix);
}
16 changes: 16 additions & 0 deletions dmemory-warning-dialog/src/processinfodelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PROCESSINFODELEGATE_H
#define PROCESSINFODELEGATE_H

#include <QItemDelegate>

class ProcessInfoDelegate : public QItemDelegate
{
Q_OBJECT

public:
explicit ProcessInfoDelegate(QObject *parent = nullptr);

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // PROCESSINFODELEGATE_H
Loading

0 comments on commit 0b50c15

Please sign in to comment.