externalprogressbar_mac.mm 3.38 KB
Newer Older
Jonathan Riddell's avatar
Jonathan Riddell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2016 ROSA
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
////////////////////////////////////////////////////////////////////////////////
// Mac OS X implementation of ExternalProgressBar


#include <QWidget>

#include "externalprogressbar.h"

#include <Cocoa/Cocoa.h>

// Class with platform-specific data
class ExternalProgressBarPrivate
{
public:
    ExternalProgressBarPrivate();
    ~ExternalProgressBarPrivate();

    NSProgressIndicator* progressIndicator;
};

ExternalProgressBarPrivate::ExternalProgressBarPrivate() :
    progressIndicator(nil)
{
}

ExternalProgressBarPrivate::~ExternalProgressBarPrivate()
{
}


ExternalProgressBar::ExternalProgressBar(QWidget* mainWindow) :
    d_ptr(new ExternalProgressBarPrivate()),
    m_MaxValue(0)
{
    Q_UNUSED(mainWindow);

    // Create a new view based on the current application icon with progress bar positioned over it
    // and replace the Dock icon with this view
    NSImageView* iv = [[NSImageView alloc] init];
    [iv setImage: [[NSApplication sharedApplication] applicationIconImage]];
    [[NSApp dockTile] setContentView: iv];

    d_ptr->progressIndicator = [[NSProgressIndicator alloc] initWithFrame: NSMakeRect(0, 0, [NSApp dockTile].size.width, 20)];
    [d_ptr->progressIndicator setHidden: YES];
    [d_ptr->progressIndicator setStyle: NSProgressIndicatorBarStyle];
    [d_ptr->progressIndicator setIndeterminate: NO];
    [d_ptr->progressIndicator setBezeled: YES];
    [iv addSubview: d_ptr->progressIndicator];

    [d_ptr->progressIndicator release];

    [[NSApp dockTile] display];
}

ExternalProgressBar::~ExternalProgressBar()
{
    DestroyProgressBar();
    delete d_ptr;
}

// Initializes the external progress bar and sets its limits
bool ExternalProgressBar::InitProgressBar(quint64 maxSteps)
{
    m_MaxValue = maxSteps;

    [d_ptr->progressIndicator setMinValue: 0];
    [d_ptr->progressIndicator setMaxValue: maxSteps];
    [d_ptr->progressIndicator setDoubleValue: 0];
    [d_ptr->progressIndicator setHidden: NO];
    [[NSApp dockTile] display];

    return true;
}

// Deinitializes the external progress bar and returns into the normal state
bool ExternalProgressBar::DestroyProgressBar()
{
    [d_ptr->progressIndicator setHidden: YES];
    [[NSApp dockTile] display];
    return true;
}

// Updates the current progress bar position
bool ExternalProgressBar::SetProgressValue(quint64 currentSteps)
{
    [d_ptr->progressIndicator setDoubleValue: currentSteps];
    [d_ptr->progressIndicator setHidden: NO];
    [[NSApp dockTile] display];

    return true;
}

// Sets the progress bar to indicate pause
bool ExternalProgressBar::ProgressSetPause()
{
    // Not supported by OS X
    return false;
}

// Sets the progress bar to indicate an error
bool ExternalProgressBar::ProgressSetError()
{
    // Not supported by OS X
    return false;
}